Thursday, September 12, 2013

Cripto Class


Some years ago after reading the Simon Singh book's "The Secret History of Codes and Code-breaking" I created my first Cripto class. The main idea was a kind of Enigma code, with simple tricks to improve it. In the time this class was used always more. In this article I present the class and the contained ideas.

public class Cripto
    {
        const int SIZE_ALPHABET = 256;
        const int LOWER_BOUND = 32;

        byte[] msKeyWord = new byte[] {0x49, 0x76, 0x60, 0x6e, 0x31, 0x4d, 
            0x3f, 0x60, 0x78, 0x43, 0x64, 0x65, 0x74, 0x5b, 0x6e, 0x7f};
        string msLastCriptedStr = "";

This class uses a key phrase (KP) that can have lenght as you want, in the example is 13 chars as default.
For security reason this key phrase is expressed in Hexadecimal and stored on an array. The german army had a book with these key phrases, every day had a different phrase. There is a nice movie directed by Michael Apted from a screenplay by Tom Stoppard about "Enigma" that  I suggest to watch. Alan Touring was also involved in decoding it and many spy stories talk about that tricky code.

It is possible to change this KP when the object is created.

public Cripto(string aKeyWord)
        {
            if (aKeyWord != "")
                msKeyWord = System.Text.Encoding.Unicode.GetBytes(aKeyWord);
        }

The main methods are simply two: Encrypt() and Decrypt() they returns as result a string.



public string Encrypt(string aText)
        {
            int apos; //alphabet pos
            int npos; //new coded pos
            int kwi = 0; //KeyWord Index
            int kwa; //KeyWord alphabet
            string s = ""; //coded string

            int l = aText.Length; //string length
            for (int i = 0; i < l; i++)
            {
                kwa = (int)msKeyWord[kwi]; //choose cripto alphabet
                kwi = kwi + 1; //set keyword index
                //if last key character restart from first position
                if (kwi == msKeyWord.Length) kwi = 0;

                apos = (int)aText[i];
                npos = (kwa + apos) % SIZE_ALPHABET;
                s = s + (char)npos; //multi alphabet traslation                
            }

            msLastCriptedStr = s;
            return s;
        }

public string Decrypt(string aText)
        {

            int l; //i=string pos;
            int apos; //alphabet pos
            int npos; //new coded pos
            int kwi = 0; //KeyWord Index
            int kwa; //KeyWord alphabet
            string s = ""; //coded string

            l = aText.Length; //string length
            for (int i = 0; i < l; i++)
            {
                kwa = (int)(msKeyWord[kwi]); //chose decripto alphabet
                kwi = kwi + 1; //set keyword index
                //if last key character restart from first position
                if (kwi == msKeyWord.Length) kwi = 0;

                apos = (int)(aText[i]);
                npos = (SIZE_ALPHABET + apos - kwa) % SIZE_ALPHABET;
                s = s + (char)(npos); //multi alphabet traslation                
            }

            return s;
        }

How the algorithm works: Every chars of your string is replaced with one new letter. This letter is taken each time from different alphabet (it depend on KP). The char position is the same of the examined input string character. The KP provides a sequence of index that are the characters position of the first alphabet.



No comments :

Post a Comment