Monoalphabetic Cipher In C#

In my previous article, I have explained one of the methods of substitution techniques, i.e. Caesar Cipher. Today, we will discuss another one which is more advanced than Caesar Cipher, called Monoalphabetic Cipher.

In Monoalphabetic Cipher, a drawback of the key of Caesar cipher has been improved with the help of permutation. Before going further, we should understand the meaning of permutation – Permutation of a finite set of elements. S is an ordered sequence of all the elements of S, with each element appearing exactly once.

In general, there are n! Permutations of a set of n elements, because the first element can be chosen in one of n ways, the second in n-1 ways, the third in n-2 ways, and so on.

So, we can say “the way to arrange the things” is permutation. Here, we are more concerned towards the key, and therefore we are permutating the key.

From the Permutation Definition we can take the below example to understand.

S = {a, b, c}, then there will be 6 possible ways in which the elements of S can be arranged.
S = {abc, acb, bac, cab, cba} – these are the 6 possible ways.

It is quite similar to solving a factorial. Here, 3! is given, so 3! = 3*2*1 = 6 possible ways.

We had seen in Caesar cipher that we used only a single key to encrypt the data and again the same key to decrypt the data, but Monoalphabetic is an improved substitution cipher, where we are using 26 keys of the alphabet.

Moreover, 26 keys has been permuted to 26! Or greater than that – 4*10^26 possible keys – which is quite a bit higher than the key space of DES (Data Encryption Standard). Now, with such huge key space, the Monoalphabetic cipher eliminates brute force techniques for cryptoanalysis.

Venture of Monoalphabetic Cipher

Well! With key space, we eliminate the brute force problem, but that doesn’t mean that the Monocipher is unbreakable. There is another line of attack that makes Monoalphabetic cipher an “inefficient cipher”.

If the cryptoanalyst knows the nature of plaintext which is nothing but a “Non-compressed English Text”, then the analyst has a great chance to exploit the plaintext. To understand it more, let’s take a huge Ciphertext.

UZQSOVUOHXMOPVGPOZPEVSGZWSZOPFPESXUDBMETSXAIZ
VUEPHZHMDZSHZOWSFPAPPDTSVPQUZWYMXUZUHSX
EPYEPOPDZSZUFPOMBZWPFUPZHMDJUDTMOHMQ


Now, what exactly analysts do here is- they analyze the frequency of the letters and then compare it with the Standard Frequency Distribution chart for English. In that way, they can proceed to the plaintext.



The above figure shows you the relative frequencies of Ciphertext in percentage. Now you just have to match the percentage of letters from the above chart with the percentage of letters to the standard frequency distribution chart. Below figure is- Standard Frequency Distribution chart for English.



You can see the letter “P” and “Z” have the highest frequency in Ciphertext, you can match their frequency in the standard distribution chart and get the plaintext.

Ciphertext Letter Ciphertext letter
Frequency
Related Plaintext Letter Frequency Related Plaintext Letter
P 13.33 ~12.02 e
Z 11.67 ~9.06 t

So if we go with the similar fashion – we got half of the plaintext, from which we can extract the information. You get plaintext something like below image.


This technique is more worthy if the Ciphertext is in a long sequence, because in long sequence you get more letters to evaluate; in short messages we cannot expect an exact match.

Hope, you understand the basic of Monoalphabetic Cipher, now we will implement Monoalphabetic cipher in C#.

Monoalphabetic cipher in C#

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace PolyalphabeticCipher  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             string key = "zyxwvutsrqponmlkjihgfedcbaABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  13.   
  14.   
  15.             Console.WriteLine("Enter your String:");  
  16.             Console.Write("\n");  
  17.             string plainText = Console.ReadLine();  
  18.             Console.Write("\n");  
  19.   
  20.             string cipherText = Encrypt(plainText, key);  
  21.             Console.WriteLine("Your Encrypted Data:\t"+cipherText);  
  22.   
  23.             Console.Write("\n");  
  24.   
  25.             string decryptedText = Decrypt(cipherText, key);  
  26.             Console.WriteLine("Your Decrypted Data:\t"+decryptedText);  
  27.             Console.ReadKey();  
  28.   
  29.         }  
  30.   
  31.   
  32.          static string Encrypt(string plainText, string key)  
  33.         {  
  34.             char[] chars = new char[plainText.Length];  
  35.             for (int i = 0; i < plainText.Length; i++)  
  36.             {  
  37.                 if (plainText[i] == ' ')  
  38.                 {  
  39.                     chars[i] = ' ';  
  40.                 }  
  41.   
  42.                 else  
  43.                 {  
  44.                     int j = plainText[i] - 97;  
  45.                     chars[i] = key[j];  
  46.                 }                
  47.             }  
  48.   
  49.              return new string(chars);  
  50.         }  
  51.   
  52.   
  53.         public string reverse(string cipherText)  
  54.         {  
  55.             char[] charArray = cipherText.ToCharArray();  
  56.             Array.Reverse(charArray);  
  57.   
  58.             return new string(charArray);  
  59.         }  
  60.   
  61.   
  62.         static string Decrypt(string cipherText, string key)  
  63.         {  
  64.             char[] chars = new char[cipherText.Length];  
  65.             for (int i = 0; i < cipherText.Length; i++)  
  66.             {  
  67.                 if (cipherText[i] == ' ')  
  68.                 {  
  69.                     chars[i] = ' ';  
  70.                 }  
  71.                 else  
  72.                 {  
  73.                     int j = key.IndexOf(cipherText[i]) + 97;  
  74.                     chars[i] = (char)j;  
  75.                 }  
  76.             }  
  77.             return new string(chars);  
  78.         }  
  79.     }  
  80. }  
Encryption Code

So we are dealing with “nilesh” as plaintext, in the first line of the encrypt method, we will store the letters to an array, then letter by letter we will use it from that array to encrypt the whole plaintext. Our “For” loop will check the plaintext length, inside that “if” condition -checks the whitespace in the plaintext.

Next in else condition.

int j = plainText[i] - 97;

Plaintext[i] has some letters from the plaintext, which is having ASCII value, we will take the difference - plainText[i] – 97, to get the Ciphertext letter.

Let's say “n” will come from the plaintext “nilesh”. “n” is having ASCII as –> 110, So 110-97 = 13. In the next line key[j] (Key [13]) will give you the Ciphertext letter by referring from the key below.

string key = "zyxwvutsrqponmlkjihgfedcba”

From the given key, we get the cipher letter as “M” for the Plaintext letter “N”. To do for other letters in a similar fashion you get the below ciphertext.

 

Plaintext Ciphertext
N M
I R
L O
E V
S H
H S

For Decryption


Decryption is the very opposite of encryption, here you just have to change the operand from difference to addition, which gives you the plaintext back.

int j = key.IndexOf(cipherText[i]) + 97;

OUTPUT WINDOW



Hope you liked it! Thank you for Reading, have a good day!


Recommended Free Ebook
Similar Articles