Site hosted by Angelfire.com: Build your free website today!

Encrypt
Home Up High Level Design Layout of Controls Encrypt Decrypt Making Encrypt/Decrypt Work Using the Application

 

How It Works
Playing With Binary

All About Our Encryption Algorithm

Here we discuss the encryption algorithm used in the final project.  We will be using a very simple encryption algorithm.

Any file is a sequence of numbers.  Even text files, ultimately, are a sequence of numbers.  Each number in a text file represents a character.  When a control, such as a text box, displays one of these numbers, it displays it as a character.  The utilities we will use to read and write files translates these files into a string.  Each character in the string corresponds to a number stored in the file and the order of the characters in the string is the same as the order of the corresponding numbers in the file which represent the characters.

Normally, when reading in text, we do not see the numbers, we see only the "characters" they represent.  Visual Basic provides functions to translate these characters to their numerical representations and to translate their numerical representations back to characters.

By now, you should know that all numeric values processed by digital computers are represented in binary.  Each numerical value is represented as a sequence of ones and zeros.  In turn, as stated above, characters are represented as numeric values.  Thus, we have the following.

Character = Numeric Value = Binary Value = Sequence of Ones and Zeros

For example, the character 'W' is represented as the decimal number 87.  The decimal number 87 is represented in binary as 1010111.  While I could explain how to translate between decimal and binary here, how we make the conversion is not important for our example because the bitwise operators we use in Visual Basic automatically view numeric values as binary.  For those of you interested in learning how to convert from decimal to binary, read this.

If you wish to play with converting decimal to binary values (and back) for your own amusement (not necessary for this project), click here.

In the diagram below, we use characters to show how our encryption algorithm works.  However, under the covers, binary is used.  Below the diagram, we explain how a single character is encrypted using the binary representation rather than the character representation.  The same process is applied to each and every character in the text, therefore there is no need to provide more than a couple of examples.

The above diagram explains, at a high level, how our encryption algorithm works.  The sequence of squares, displayed as the top row in the diagram, represents our key.  The key is the secret code the user enters when he or she wishes to encode his or her document such that no one else can read decode it.  In our example above, the key is "XYZ".  The first three squares holding the first three characters of the key have dark black sides.  The repeated sequence of the key to the right uses light gray boxes.  This is to emphasize that the key, in this case, is only three characters long and that the sequence following it to the right is merely a repetition, over and over again, of the same key.

The next row of boxes represents the text of the file to be encrypted.  This text is not repeated.  Each character in the row corresponds to a character in the file and the position of the character in the row is the same as the position of the character in the file.

To encrypt, we combine the binary code of each character in the text with the corresponding character in the key.  To perform this combination, we use the bitwise operator XOR.  XOR, performs the binary operation XOR on pairs of bits.  Given two sequences of bits (e.g. 01 and 11), XOR performs the bit operation XOR on the first bit of each pair (e.g. 0, 1) and then on the second bit of each pair (e.g. 1, 1).  It does this for every bit in the two binary numbers it combines.  If there are 8 bits in each sequence, it performs the operation eight time.

The bit operation XOR is described in the following diagram:

Let's try this out with the two bit example we mentioned before the diagram.

01 XOR 11 = 10

This is how we computed the result.

0 XOR 1 = 1
1 XOR 1 = 0

Reading the ones and zeros in the above example VERTICALLY, from top to bottom, we have

01 XOR 11 = 10

If our number was eight bits long, we would apply the XOR operator eight times.

For example.

The character W in binary is 01010111.

The character X in binary is 01011000.

If we were to calculate X XOR W, we would calculate 01011000 XOR 01010111.  Use the table in the diagram above to confirm the calculation below.

0 XOR 0 = 0
1 XOR 1 = 0
0 XOR 0 = 0
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 1 = 1
0 XOR 1 = 1 

Therefore 01011000 XOR 01010111 = 00001111

It turns out that the character represented by 00001111 is not printable (it is <shift in>).

If we were to use characters instead of binary to represent the values in our example, we would have

X XOR W = <shift in>

Applying the same process to each and every character in the original text, we arrive at the encrypted text.