Sunday 10 July 2016

Rail Fence

Hello everyone. In my previous blogs, I have discussed about two different
substitution based cryptographic algorithms. That's not the end of it. There
are more. But I won't be discussing anymore algorithms of substitution
technique. You will get enough information about other algorithms in the
Wikipedia. These blogs are meant to give you the idea of cryptography.
If you face any problem while implementing them, then you can definitely
ping me in my email id.

But for now, let's move on to Transposition Technique. As the name already
suggests, it is the rearrangement of the letters. It does not involve any
replacement of characters, just jumbling of characters.

There are many algorithms which is based on Transposition Technique.
Like Rail Fence technique, Simple columnar transposition, Simple
transposition with multiple rounds, etc.

So, for the simplicity I will start with the Rail Fence algorithm.
In this technique, the characters of the plain text are written in
diagonal form at first. This arrangement forms two rows which
resembles the rail track. That's why it is called as Rail Fence.
After the two rows are produced, the cipher text is read as
row-wise.

For example,

Plain Text : Windows

Arrangement :



Cipher Text : Wnosidw

Here is the C# code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfAppCryptography.ViewModel;
namespace WpfAppCryptography.Algorithms.TechniqueBased.Transposition
{
    public class RailFence : Algorithm
    {
        public new string AlgorithmName = "Rail Fence";
        public override string Decrypt(string ciphertext, string key)
        {
            string plaintext = null;
            int j = 0, k = 0, mid;
            char[] ctca = ciphertext.ToCharArray();
            char[,] railarray = new char[2, 100];
            if (ctca.Length % 2 == 0)
                mid = ((ctca.Length) / 2) - 1;
            else
                mid = (ctca.Length) / 2;
            for (int i = 0; i < ctca.Length; ++i)
            {
                if (i <= mid)
                {
                    railarray[0, j] = ctca[i];
                    ++j;
                }
                else
                {
                    railarray[1, k] = ctca[i];
                    ++k;
                }
            }
            railarray[0, j] = '\0';
            railarray[1, k] = '\0';
            for (int x = 0; x <= mid; ++x)
            {
                if (railarray[0, x] != '\0')
                    plaintext += railarray[0, x];
                if (railarray[1, x] != '\0')
                    plaintext += railarray[1, x];
            }
            return plaintext;
        }
        public override string Encrypt(string plaintext, string key)
        {
            string ciphertext = null;
            int j = 0, k = 0;
            char[] ptca = plaintext.ToCharArray();
            char[,] railarray = new char[2, 100];
            for (int i = 0; i < ptca.Length; ++i)
            {
                if(i%2 == 0)
                {
                    railarray[0, j] = ptca[i];
                    ++j;
                }
                else
                {
                    railarray[1, k] = ptca[i];
                    ++k;
                }
            }
            railarray[0, j] = '\0';
            railarray[1, k] = '\0';
            for (int x = 0; x < 2; ++x)
            {
                for (int y = 0; railarray[x, y] != '\0'; ++y)
                    ciphertext += railarray[x, y];
            }
            return ciphertext;
        }
    }
}

Although it looks very simple. But it is highly useful for Encryption process.
You can merge Substitution & Transposition techniques to create your own
cryptographic algorithm.



I hope you find my blogs useful. Stay tuned and keep coding.

Practice Page Link :
http://handsoncrypto.azurewebsites.net/PracticePage.aspx

No comments:

Post a Comment