The Haunted House

Sunday, July 01, 2007

Fun With Cryptography

Last year I watched the Thomas Harris film adaptation of his World War 2 novel, Enigma. The film was about Alan Turin's work in cracking the German Enigma cipher system. This got me thinking about different type of text encryption techniques. So one weekend when my wife was away on business, and I had nothing else to be getting on with, I started writing some simple text encoding routines.

The first routing in the sample code is a monophonic substitution cipher. This simply substitutes a different letter for each letter of the alphabet.

MonoSubstitute mono = new MonoSubstitute(KeyType.USER_KEY, true);
mono.PlainText = ”Text to encrypt”;
mono.Key = "This is my key";

Next up we have homophonic encoding. This assigns 2 values per letter of the alphabet. This helps even out the spread of characters to make the encoding more secure against attacks. Each letter of the alphabet is assigned 2 numbers.

HomophonicCoding homo = new HomophonicCoding(KeyType.USER_KEY);
homo.Key = "This is my key";
homo.SubKey = "This is my sub key";
homo.PlainText = ”Text to encrypt”;

When a monophonic cipher is used, the frequency histogram of the cryptogram is made flatter by increasing the size of the alphabet. This ensures that more than one ciphertext character may represent the same plaintext character. However there is always the danger of an attacker compiling a dictionary of known plaintext and ciphertext pairs for a given key.

Another way of achieving the objective of flattening the frequency histogram is by the use of a polyalphabetic cipher. When a polyalphabetic cipher is used, the ciphertext character replacing a particular plaintext letter may vary through the cryptogram and might, for instance, depend on its position in the plaintext message or the content of the plaintext that precedes it.

The final algorithm shown in the sample is the Vigenere cipher, invented by Blaise de Vigenere, a 16th century French diplomat. The Vigenere uses a vigenere square to perform encryption. The left hand (key) column of this square contains the English alphabet, and for each letter, the row determined by that letter contains a rotation of the alphabet with that letter as the leading character. So that each letter in the left hand column gives a Caesar cipher whose shift is determined by that better.

VigenereCipher vig = new VigenereCipher();
vig.Key = "This is my key";
vig.PlainText = ”Text to encrypt”;

Also included within the sample source code is a combined cipher that uses a combination of all 3 encryption type. I’ll leave it as an exercise for the reader to see how this all works.

CombinedCipher com = new CombinedCipher();
com.Key = "This is my key";
com.PlainText = ”Text to encrypt”;
com.SubKey = "This is my sub key";

I’m sure I don’t need to say this, but I will mention it anyway. These are very old types of text encryption that by todays standards are not secure, so please don’t use them for encrypting anything important. The code was only written for fun.

The source code is written in C# and the solution file was created with Visual Studio 2005. There are some unit tests in CryptTests.cs. These can be run using the NUnit test runner or the Resharper test runner.



Post a Comment

<< Home