1. Home
  2. Computing & Technology
  3. Visual Basic

Very Simple Encryption using VB.NET
How the About VB Simple Encryption Program Works

By Dan Mabbutt, About.com

The "encryption key file" is created by writing the contents of an array of characters (Char) to a file. The array is 256 characters long to allow it to accept any character. (Remember that arrays are zero based in .NET.)

Dim CodeKeyArray(255) As Char

In this version however, only 95 "standard" ASCII characters are used in the key file. The others are replaced with a blank. In addition, this version allows a backspace to correct a typing error in the TextBox. If you decide to use this same logic for some other file, such as a binary file, the statements that test for non-standard characters should be deleted.

The heart of the processing is the InBox_KeyPress event subroutine. Whenever a key is pressed, standard ASCII characters are passed to the Stash subroutine.

If Asc(e.KeyChar) = 8 Then ' Backspace
   CodeBox.Text = _
   Microsoft.VisualBasic.Left(CodeBox.Text, Len(CodeBox.Text) - 1)
Else
   If Asc(e.KeyChar) > 126 Or Asc(e.KeyChar) < 32 Then e.KeyChar = " "
   ' This statement writes the coded key value to the key file
   CodeBox.Text &= Stash(e.KeyChar)
End If

The Stash subroutine first checks to see if this particular input character already has a coded substitution value in CodeKeyArray. In .NET, the value Nothing represents the unintialized default value of any variable.

If CodeKeyArray(Asc(InChar)) = Nothing Then

If a value already exists in the array, it's just passed back in the Else clause. But if no value exists, then Stash goes to work generating a new random character. But first, Stash has to check to see it that particular random character has already been generated before. If the character can't be found in CodeKeyArray, then it's new.

If this routine was put to use in a completely binary file, this loop could take a lot of time as the last few elements of CodeKeyArray are filled in and it might be a good idea to create the array in advance rather than one character at a time as I do here. This is also a good example of where Loop Until works better than other loops since we want to execute the random character generation statement at least once every time.

Do
   StashChar = Chr(CInt(255 * Rnd()))
Loop Until InStr(CStr(CodeKeyArray), StashChar) = 0

When the user decides to save the code files for the text message, the new .NET 2.0 My namespace is used to save the files. These statements write the files to the same directory that the program is in. One improvement that could be coded would be to code a way to write the files to a more appropriate location.

My.Computer.FileSystem.WriteAllText( _
   "Stash.txt", CodeBox.Text, False)
My.Computer.FileSystem.WriteAllText( _
   "StashCode.txt", CodeKeyArray, False)

These are the files that have to be used in the DeCode program to get the original text back again. That program does doesn't have to do much work at all and the My namespace makes the work that is done easier.

Dim CodedFile As String
CodedFile = _
   My.Computer.FileSystem.ReadAllText("Stash.txt")
CodeKeyArray = _
   My.Computer.FileSystem.ReadAllText("StashCode.txt")
For I = 0 To Len(CodedFile) - 1
   DecodedBox.Text &= Chr( _
      InStr(CodeKeyArray, CodedFile(I)) - 1)
Next

Download the source code for both the QuickCode and QuickDeCode.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Very Simple Encryption using VB.NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.