Security

Rail-Fence Cipher C# Source Code

The rail-fence cipher algorithm has been posted before so this post just show how to implement the algorithm to programming language in C#. First of all always the encryption method:

public static string Encrypt(int rail, string plainText)
{
    List<string> railFence = new List<string>();
    for (int i = 0; i < rail; i++)
    {
        railFence.Add("");
    }

    int number = 0;
    int increment = 1;
    foreach (char c in plainText)
    {
        if (number + increment == rail)
        {
            increment = -1;
        }
        else if (number + increment == -1)
        {
            increment = 1;
        }
        railFence[number] += c;
        number += increment;
    }

    string buffer = "";
    foreach (string s in railFence)
    {
        buffer += s;
    }
    return buffer;
}

Read More →

Rail-Fence Cipher

Rail-Fence Cipher is one of the simplest encryption method. The method just write down the next every character diagonally up and down. For example if we encrypt “I Made Krisna” with 4 rail it will look like ( dot means null):

 I  .  .  .  .  .     .  .  .  .  .  a  .  .  .  .  .  i  .  .  .  .  .
 .     .  .  .  e  .  K  .  .  .  n  .     .  .  .  h  .  a  .  .  .  a
 .  .  M  .  d  .  .  .  r  .  s  .  .  .  W  .  d  .  .  .  s  .  r  .
 .  .  .  a  .  .  .  .  .  i  .  .  .  .  .  i  .  .  .  .  .  t  .  .

Then the ciphertext can be created with reading it normally line by line. And the result of ciphertext:

I ai eKn haaMdrsWdsraiit

Now for decryption algorithm, many way to decrypt this cipher text but there is no standard like encrypting method so i use my own method to decrypt. The main idea is creating the same pattern with encrypting algorithm but the elements of rail-fence is not the character but the index of cipher text on plain text.

 1  .  .  .  .  .  7  .  .  .  .  . 13  .  .  .  .  . 19  .  .  .  .  .
 .  2  .  .  .  6  .  8  .  .  . 12  . 14  .  .  . 18  . 20  .  .  . 24
 .  .  3  .  5  .  .  .  9  . 11  .  .  . 15  . 17  .  .  . 21  . 23  .
 .  .  .  4  .  .  .  .  . 10  .  .  .  .  . 16  .  .  .  .  . 22  .  .

Then we can create a sequence that will reordering the location of cipher text to plain text as:

 I     a  i     e  K  n     h  a  a  M  d  r  s  W  d  s  r  a  i  i  t
 1  7 13 19  2  6  8 12 14 18 20 24  3  5  9 11 15 17 21 23  4 10 16 22

 I     M  a  d  e     K  r  i  s  n  a     W  i  d  h  i  a  s  t  r  a
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24