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

Very Simple Encryption using VB.NET
A lesson in string manipulation with a handy encryption utility as a bonus!

By Dan Mabbutt, About.com

In today's security conscious online world, encryption has become a major topic. And the software vendors have responded in force. There are lots of sophisticated and virtually unbreakable encryption tools out there such as Microsoft's Encrypting File System (EFS) to third-party solutons like AxCrypt, PGP, and Encrypt Easy. You can encrypt emails and Zip files automatically if you want to. In fact, it's relatively easy to encrypt your data so thoroughly that even you can never access it again! (Something that people discover for themselves the hard way again and again.)

The common problems with all of these solutions is that they require setup and installation. And they can be a lot more difficult to use than the software vendor ads promise. You usually need a "digital certificate" for the best encryption. The best encryption clearly works, but it can be a chore to get it to work.

To avoid this problem, there are also a number of really simple encryption ideas out there. One of these is called "exclusive OR" (XOR) encryption. The idea is that you XOR a file byte by byte to produce something that looks like a jumbled mess. But all you have to do is XOR the same file to reverse the encryption. There's an assembly language routine that can implement this method in 42 bytes! Something this simple (and easy to crack) hardly deserves the name encryption.

This article will describe a "middle ground" solution. Like XOR encryption, our solution will process a file byte by byte and produce a simple encrypted file that certainly can't be interpreted visually. But like more sophisticated encryption, the file will be encrypted using a random key file that is unique in every case. To decrypt the file, you need both the encrypted file and the key. But our solution doesn't produce high-security encryption. Common cracking techniques can be applied and the encryption can be broken relatively easily. In other words, it's good for keeping your mother-in-law at bay, but not the security department of a corporation.

Encryption is only a side benefit, however. The real purpose of this program and this article is to give you a real and practical example of VB.NET string manipulation techniques. If you study this program thoroughly, you should have no trouble coding your own string manipulation.

The way this program works is to type a message into a TextBox. The unencrypted and encrypted versions of the message appear simultaneously. When you click the Stash It! button, an encrypted file and a key file are written to disk. Then you can keep the two files seperate and the information in the encypted file will be relatively safe from anyone who doesn't have the key file. To decrypt again, just input both programs into a very simple decryption program that we'll also write here and the original information is recovered. The process is shown in the graphic below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The logic of the program is just as simple. A random key value is saved in the key file at an index location of the ASCII value of the input character. This random key value is then saved in the encypted file for that same character. To decrypt, the program just finds an input character in the key file. The index value of that location is the ASCII value of the decoded character.

Here's a flowchart showing the process for one character:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The program in this article is a "bare bones" version. For example, text has to be typed into a TextBox rather than input from a file for encryption. For decryption, there's no way to select the files for input. (For testing, you have to copy the encrypted and key files from the Bin directory of one application to the Bin directory of the other.) But it wouldn't be much of a modification to add all of these things and you can use the core program code without changing anything.

See how it works and download the source on the next page!

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.