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-
- 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, 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.