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.



Friday, September 6, 2013

Message Grid Component


Another component that cannot miss on your toolbox is a Form where to append all messages.
When I talk about messages, I mean debug messages, user messages, error messages. This form is used, typically, on automation program or calculation and analysis application but also in many other contexts.
By the way, it is really a useful component. What it makes, it is simply said: first of all defines some methods to append a message on a list and visualize it, then allows to save all rows on a text file, a log file.



Message Class

A Message class has four main properties:
• The Type that can be an enumerative value Error, Warning or Infos
• A Code. Often is useful assign a code to each message.
• A Text: the description.
• A Count: For example if an error appears systematically, it can be useful update a counter and a date, insted to append it again. While on file the message is always saved.

MessagesCollection Class

The MessagesCollection class, manage a list of Message objects with the possibility to limit a number of these. It save every new message on a log text file.
This is a constructor declaration:

public MessagesCollection(string asFileName, int aiMaxElements)


It defines a simple method to add a message:
public void Add(EnumMessageType aoType, string asCode, string asMessage, bool abAddOnlyIfNew = true)

or to clear a list of these:
public void Clear()

public void Clear(string asCode)


MessagesGrid Control

At the end, we have a grid for a front end visualization. For the developer it is possible to access the MessagesCollection object by the property Messages, it can use the Add method as well. All other user activity can be done from the Form.


download sample