Cryptography - Rail Fence

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

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 the characters, just jumbling of the characters.

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

For simplicity, I will start with 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. This is the reason why it is called Rail Fence. After the two rows are produced, the cipher text is read row-wise.

For example,

  • Plain Text : Windows.
  • Arrangement : W n o s i d w.
  • Cipher Text : Wnosidw.
C# code is given below-
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using WpfAppCryptography.ViewModel;  
  7. namespace WpfAppCryptography.Algorithms.TechniqueBased.Transposition  
  8. {  
  9.     public class RailFence: Algorithm {  
  10.         public new string AlgorithmName = "Rail Fence";  
  11.         public override string Decrypt(string ciphertext, string key) {  
  12.             string plaintext = null;  
  13.             int j = 0, k = 0, mid;  
  14.             char[] ctca = ciphertext.ToCharArray();  
  15.             char[, ] railarray = new char[2, 100];  
  16.             if (ctca.Length % 2 == 0) mid = ((ctca.Length) / 2) - 1;  
  17.             else mid = (ctca.Length) / 2;  
  18.             for (int i = 0; i < ctca.Length; ++i) {  
  19.                 if (i <= mid) {  
  20.                     railarray[0, j] = ctca[i];  
  21.                     ++j;  
  22.                 } else {  
  23.                     railarray[1, k] = ctca[i];  
  24.                     ++k;  
  25.                 }  
  26.             }  
  27.             railarray[0, j] = '\0';  
  28.             railarray[1, k] = '\0';  
  29.             for (int x = 0; x <= mid; ++x) {  
  30.                 if (railarray[0, x] != '\0') plaintext += railarray[0, x];  
  31.                 if (railarray[1, x] != '\0') plaintext += railarray[1, x];  
  32.             }  
  33.             return plaintext;  
  34.         }  
  35.         public override string Encrypt(string plaintext, string key) {  
  36.             string ciphertext = null;  
  37.             int j = 0, k = 0;  
  38.             char[] ptca = plaintext.ToCharArray();  
  39.             char[, ] railarray = new char[2, 100];  
  40.             for (int i = 0; i < ptca.Length; ++i) {  
  41.                 if (i % 2 == 0) {  
  42.                     railarray[0, j] = ptca[i];  
  43.                     ++j;  
  44.                 } else {  
  45.                     railarray[1, k] = ptca[i];  
  46.                     ++k;  
  47.                 }  
  48.             }  
  49.             railarray[0, j] = '\0';  
  50.             railarray[1, k] = '\0';  
  51.             for (int x = 0; x < 2; ++x) {  
  52.                 for (int y = 0; railarray[x, y] != '\0'; ++y) ciphertext += railarray[x, y];  
  53.             }  
  54.             return ciphertext;  
  55.         }  
  56.     }  
  57. }  
Although, it looks very simple, it is highly useful for encryption process. You can merge Substitution & Transposition techniques to create your own cryptographic algorithm.

Here is the link for the video.

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