DotNet Tutorials

Server Intellect

 Using WinForms to Encrypt a String in VB.NET

This tutorial will show you how to write a method that will not only encrypt a string - but decrypt it as well. Written with VS.NET 2008 and VB.NET

This example was created with Visual Studio .NET 2008 but the results can be achieved in 2005 also.

Having the ability to encrypt data is extremely useful, as we often deal with sensitive material. In this tutorial, you will learn how to write a method that will both encrypt and decrypt a string. We will be creating a Windows Form that will allow us to enter a string into a textbox and encrypt it, then decrypt it back to the original string.

The first thing we will do is to create a new project, and add the following controls to the form: Two labels, two textboxes, and two buttons. Each one of these controls is for the encryption, one for the decryption. We will set the visibility of the decryption controls to false - we will make them visible when the encrypt button is clicked.

Once we have our controls in place, we will write our method:

Public Class StringEncryption
Public Shared Function SimpleEncrypt(ByVal toEncrypt As String) As String
Dim tempChar As String = Nothing
Dim i As Integer = 0
For i = 1 To toEncrypt.Length
If System.Convert.ToInt32(toEncrypt.Chars(i - 1)) < 128 Then
tempChar = System.Convert.ToString(System.Convert.ToInt32(toEncrypt.Chars(i - 1)) + 100)
ElseIf System.Convert.ToInt32(toEncrypt.Chars(i - 1)) > 128 Then
tempChar = System.Convert.ToString(System.Convert.ToInt32(toEncrypt.Chars(i - 1)) - 100)
End If
toEncrypt = toEncrypt.Remove(i - 1, 1).Insert(i - 1, (CChar(ChrW(tempChar))).ToString())
Next i
Return toEncrypt
End Function
End Class

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!

This is a relatively simple string encryption, as it performs an ASCii character swap - it is not intended for highly-sensitive data.
The method parses the string character by character, and adds to the ASCii value of each character. The IF statement checks to see if the string has already been encrypted, as the same method performs both encryption and decryption. If it has already been encrypted, the method reverses the process (subtracts for the ASCii value of each character.

We utilize this method in our Form like so:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Visible = True
Label3.Visible = True
Button2.Visible = True
TextBox2.Text = StringEncryption.SimpleEncrypt(TextBox1.Text)
TextBox1.Text = ""
End Sub

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

When the Encrypt button is clicked, we make visible the decryption controls, and call the encryption method, and assign the returned value to the decrypt textbox. This allows the user to just click the decrypt button to decrypt the string. We also set the original textbox value to nothing.

We can do the same for the decrypt button click, as the method itself will recognize if the string needs to be encrypted or decrypted:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Visible = False
Label3.Visible = False
Button2.Visible = False
TextBox1.Text = StringEncryption.SimpleEncrypt(TextBox2.Text)
TextBox2.Text = ""
End Sub

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.

We use the same structure as before, but we hide the decrypt controls again, and assign the method return value to the original textbox - the encrypted string will be decrypted, and the original text shown in the original textbox as before.

The entire code-behind will look something like this:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Visible = True
Label3.Visible = True
Button2.Visible = True
TextBox2.Text = StringEncryption.SimpleEncrypt(TextBox1.Text)
TextBox1.Text = ""
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Visible = False
Label3.Visible = False
Button2.Visible = False
TextBox1.Text = StringEncryption.SimpleEncrypt(TextBox2.Text)
TextBox2.Text = ""
End Sub
End Class

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect