Cold War Spy Algorithm For Cryptography
If you have seen "The Numbers Station" with John Cusack and Malin Akerman, then you are familiar with our next topic. The central theme
of the movie was about a sending coded messages to covert operatives using a technique similar to the One-time pad.
Wikipedia Reference: "One-time pad" Creating a cryptographic routine in software to implement this, yields a rather robust security routine.
In the spy world the key was needed to make any sense out of the message, since the control string was usually known. The control string is what is used with the key to encode/decode a message, and it was usually something along the lines of ABCDEFGH...12345...; however to computers there is no need to keep this control string. As a matter of fact it increases the level of security to randomize the control string. Kind of an over-simplified view of high-tech systems which generate an apparently random set of characters changing every delta time period and can be unlocked with a key (character sequence). So in short, the control string becomes something like AZ1$2bc%37BCde... And yes a clever person that has the compiled version of the software can still reverse engineer the control string out of it, but they still don't have the key. An encoded stand alone document will be next to impossible to crack without the control string and the key.
The first step is to cycle through the characters one by one, matching each to the control string and returning the index into the control
string and checking for embedded quotes in the text.
Using the key, for each character translated in the control string, cycle through the key and also get the index to the control string where the character from the key exists. For example:
String to translate: A Happy Cow
Control string: ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz (note: space character is at position 27)
Key string: Anubis
[A] Happy Cow A=position 1 in control string (Index 0)
[A]nubis A=position 1 in control string (Index 0)
AHappy Cow space=Index 26
A[n]ubis n=Index 13
and so on. As you cycle through the control string you also have to cycle through the key, looping back to the beginning of the key each time you pass the end.
Now that you have pairs of indexes - add them together [A,A] = 0 + 0, if the resultant number is less than the length of the control
string then use the character from the control string at the position specified by the sum of the two indexes, in this case it does
not look like much since the first encrypted character happens to be "A".
Next pair [space, n] = 26 + 13 = 39 which cooresponds to character "m". So now the encrypted string looks like: Am...
If the resulting sum of the two indexes happens to be larger than the length of the control string, then you need to divide the length of the key into the control string and get the remainder. Use the Math.DivRem statement in C#. The remainder becomes the index into the control string. The procedure is also explained on the Wikipedia site since this is over-simplified and thus maybe a bit confusing.
You can also see the need to randomize the control string since in the above example the letter [A] Happy Cow, actually cooresponded to
the same letter once encrypted. If the control string had been xAy30u9cdgfDC...; the first encrypted character would have been [x].
Once you have your routines built, it is an easy process to select a text file and encrypt it. I implemented this routine in C# and Ansi C for Linux as a means for the two platforms; one Windows and the other Linux to securely communicate. Enjoy!
By the way, check out the movie as well: "The Numbers Station", very good spy thriller!
End Of Line Man!