Friday 29 July 2016

Simple Columnar Method With Multiple Rounds

Hello everyone. This blog introduces you to another Transposition
techniques - Simple Columnar Method With Multiple Rounds.

It is a very efficient and useful transposition algorithm. There are
two forms of this algorithm. One is the "Simple Columnar Method With
One Round" or simply "Simple Columnar Method" and the other one is
the "Simple Columnar Method With Multiple Rounds". Here we are going
to discuss the latter.

In this algorithm, we write the plain text message row-by-row in a
2D array of any size( Let's say 10x5 according to the example given
below ). Then read the message in column-by-column. However, it is
not necessary to read the message in order of Column 1, Column 2,
Column 3 and so on. It can be any random order like 2, 0, 3, 1, 4
( according the example ). Thus the message obtained is the cipher
text of round 1. This cipher text is input as plain text to round 2.
This process keeps on going until the last round. You can keep the
column order same or different for every round. Here I have used
different column order for every round. This example involves
four rounds. That's why it is called Simple Columnar Method With
Multiple Rounds. The final message that we obtain after the last
round is the cipher text that is send to the receiver. The receiver
reverses the algorithm to decrypt the cipher text. On the receiver
side, the last round of the sender side becomes the first round of
receiver side and so. We write the cipher text column-by-column in
the specified column order by the corresponding round. Then we read
row-by-row to get the original message.



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 SimpleColumnarMethodWithMultipleRounds : Algorithm
    {
        public new string AlgorithmName = "Simple Columnar Method With Multiple Rounds";

        public override string Decrypt(string ciphertext, string key)
        {
            string plaintext = null;
            char[] ctca = ciphertext.ToCharArray();
            char[] itca = new char[ctca.Length];
            char[,] cells = new char[10, 5];
            int[][] rounds = new int[4][];
            rounds[0] = new int[] { 4, 1, 0, 3, 2 };
            rounds[1] = new int[] { 0, 4, 3, 1, 2 };
            rounds[2] = new int[] { 1, 4, 2, 3, 0 };
            rounds[3] = new int[] { 2, 0, 3, 1, 4 };

            itca = ctca;

            for (int i = 0; i < 4; i++)
            {
                cells = SetCharactersInCellsVertically(cells, itca.Length, itca, rounds[i]);
                itca = SetItcaFromCellsHorizontally(cells, itca);
            }

            for (int i = 0; i < itca.Length; i++)
            {
                plaintext += itca[i];
            }

            return plaintext;
        }

        public override string Encrypt(string plaintext, string key)
        {
            string ciphertext = null;
            char[] ptca = plaintext.ToCharArray();
            char[] itca = new char[ptca.Length];
            char[,] cells = new char[10, 5];
            int[][] rounds = new int[4][];
            rounds[0] = new int[] { 2, 0, 3, 1, 4 };
            rounds[1] = new int[] { 1, 4, 2, 3, 0 };
            rounds[2] = new int[] { 0, 4, 3, 1, 2 };
            rounds[3] = new int[] { 4, 1, 0, 3, 2 };

            itca = ptca;

            for (int i = 0; i < 4; i++)
            {
                cells = SetCharactersInCellsHorizontally(cells, itca.Length, itca);
                itca = SetItcaFromCellsVertically(cells, itca, rounds[i]);
            }

            for (int i = 0; i < itca.Length; i++)
            {
                ciphertext += itca[i];
            }

            return ciphertext;
        }

        private char[,] SetCharactersInCellsHorizontally(char[,] cells, int length, char[] itca)
        {
            int x = 0, y = 0;

            for (int i = 0; i < itca.Length; i++)
            {
                cells[x, y++] = itca[i];
                if (y == 5)
                {
                    y = 0;
                    ++x;
                }
            }

            return cells;
        }

        private char[] SetItcaFromCellsVertically(char[,] cells, char[] itca, int[] columnorder)
        {
            int k = 0;

            for (int x = 0; x < 5; x++)
            {
                int t = columnorder[x];
                for (int y = 0; cells[y,t] != '\0'; y++)
                {
                    itca[k++] = cells[y, t];
                }
            }

            return itca;
        }

        private char[,] SetCharactersInCellsVertically(char[,] cells, int length, char[] itca, int[] columnorder)
        {
            int k = 0;

            for (int x = 0; x < 5; x++)
            {
                int t = columnorder[x];
                for (int i = 0; ((i*5)+t) < length; i++)
                {
                    cells[i, t] = itca[k++];
                }
            }

            return cells;
        }

        private char[] SetItcaFromCellsHorizontally(char[,] cells, char[] itca)
        {
            int x = 0, y = 0;

            for (int i = 0; i < itca.Length; i++)
            {
                itca[i] = cells[x, y++];
                if (y == 5)
                {
                    y = 0;
                    ++x;
                }
            }

            return itca;
        }
    }
}


Here is the video :



That's the end of it. I hope you find my blogs useful.
See you next time. Till then keep coding.

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

No comments:

Post a Comment