This tutorial will show how we can create a method that provides quick and easy string encryption and decryption for our less sensitive data. Not recommended for highly personal data. VB.NET
Data Protection is a must these days, and if we are able to encrypt our data easily, it makes a big difference. This tutorial will show how we can create a simple method that will allow us to encrypt small amounts of data. The method uses an ASCii swap process, so it is not the most secure way of encrypting data, but it is very quick and easy to do for small amounts of data. What's more, the same method will also decrypt the data!
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
We start off our Default.aspx page in Visual Studio. We will add two textboxes and two buttons to it. We will set the second textbox and button to be invisible on page load:
| <form id="form1" runat="server"> <asp:TextBox ID="txtText" runat="server"></asp:TextBox><br /> </form> <asp:Button ID="butSubmit" runat="server" Text="Encrypt" OnClick="butSubmit_Click" /><br /> <asp:TextBox ID="txtResult" runat="server" Visible="false"></asp:TextBox> <asp:Button ID="butDecrypt" runat="server" Text="Decrypt" Visible="false" OnClick="butDecrypt_Click" /> |
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
One textbox and button will be used to encrypt data, and the other will be used to decrypt the data. From the code snippet above, you can see that we have already declared our onclick event handlers. The code behind for both buttons is as follows:
| Protected Sub butSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSubmit.Click txtResult.Text = SimpleEncrypt(txtText.Text) End SubtxtResult.Visible = True butDecrypt.Visible = True txtText.Visible = False butSubmit.Visible = False Protected Sub butDecrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSubmit.Click txtText.Text = SimpleEncrypt(txtResult.Text) End Sub txtText.Visible = True butSubmit.Visible = True txtResult.Visible = False butDecrypt.Visible = False |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
Both are identical; both buttons call the same method. This is because the method itself knows when to encrypt and when to decrypt, you will see how shortly. The buttons also show and hide the other elements, depending on if the data is being encrypted or decrypted.
Now if we take a look at the method, we will see how the encryption is performed:
| Public Function SimpleEncrypt(ByVal TheText As String) As String Dim tempChar As String = Nothing End Function Dim i As Integer = 0 For i = 1 To TheText.Length If ToInt32(TheText.Chars(i - 1)) < 128 Then Next itempChar = System.Convert.ToString(ToInt32(TheText.Chars(i - 1)) + 100) ElseIf ToInt32(TheText.Chars(i - 1)) > 128 Then tempChar = System.Convert.ToString(ToInt32(TheText.Chars(i - 1)) - 100) End IfTheText = TheText.Remove(i - 1, 1).Insert(i - 1, (CChar(ChrW(tempChar))).ToString()) Return TheText |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
The method simply loops through each character in the string provided by the user and alters it's Unicode value by adding 100 to it, which outputs underadable text. The method then detects encrypted data by checking each character to see if its Unicode value is above 128 - and if it is, it will subtract the 100 it has previously added, thus returning it to its original value and making it readable again. Now if we run this, we should be able to encrypt and decrypt any type of string. The full code-behind is as follows:
| Imports System.Convert Partial Class _Default Inherits System.Web.UI.Page End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Public Function SimpleEncrypt(ByVal TheText As String) As String Dim tempChar As String = Nothing End FunctionDim i As Integer = 0 For i = 1 To TheText.Length If ToInt32(TheText.Chars(i - 1)) < 128 Then Next itempChar = System.Convert.ToString(ToInt32(TheText.Chars(i - 1)) + 100) ElseIf ToInt32(TheText.Chars(i - 1)) > 128 Then tempChar = System.Convert.ToString(ToInt32(TheText.Chars(i - 1)) - 100) End IfTheText = TheText.Remove(i - 1, 1).Insert(i - 1, (CChar(ChrW(tempChar))).ToString()) Return TheText Protected Sub butSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSubmit.Click txtResult.Text = SimpleEncrypt(txtText.Text) End SubtxtResult.Visible = True butDecrypt.Visible = True txtText.Visible = False butSubmit.Visible = False Protected Sub butDecrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSubmit.Click txtText.Text = SimpleEncrypt(txtResult.Text) End SubtxtText.Visible = True butSubmit.Visible = True txtResult.Visible = False butDecrypt.Visible = False |
No comments:
Post a Comment