Vigenere Cipher In C#

Before reading this article, please go through my article, mentioned below.

In my previous post, we had a discussion on PlayFair Cipher in C# on today’s module. We will discuss another technique of cryptography; i.e Vigenere  Cipher, which is a Polyalphabetic Cipher.

What is Polyalphabetic Cipher?

Polyalphabetic Cipher is also based on substitution techniques, but here we are using multiple substitution alphabets, so as to increase the security context of an algorithm. We are familiar with Enigma machines, which were the most secured machine of their time, which were used in the World War II by the Nazis. Those rotor machines are also based on Polyalphabetic substitution cipher.

Enigmas Machine

Machine

These are not the only machines, but Japanese and Italians also built their alternatives to use in secure communication. Well a very nice explanation of Enigmas is shown in a movie – “Imitation Game” – in case you are curious.

Vigenere Cipher is the best known example of polyalphabetic cipher that is built by - Giovan Battista Bellaso. Vigenere cipher is a method of encrypting plaintext with the help of different Caesar ciphers which are nothing but an arrangement of alphabets.

It uses the 26x26 table of letter A- Z as the row heading and column heading. See below for Reference.

Machine

For Encryption

Suppose the plaintext is MICHIGAN

Machine

Now if suppose your plaintext is a long string, then you have to extend your key to the plaintext. See below table

Machine

As you can see the key is repeated, it will be similar in size to that of plaintext like above. Now you have to divide the result into 5 letter blocks.

Machine

To encrypt the following plaintext and its corresponding letter, we have to use it above a 26X26 matrix, use the keyword letter and plaintext letter as the row index and column index, and the entry where row and column intersect is the letter of Ciphertext to that particular letter of plaintext.

Now as per the plaintext – first letter is “M” and from the key the first letter is “H”. Now you have to look up in the table – labelled as Plaintext letters in the left and top letters are the key. Where ever both intersect is your cipher letter – in case of ours it is “T”

Machine

KEY

KEY

With the help of the above method, you can find the rest of the letter, so the cipher will look as shown below

KEY

Implementation of Vigenere Cipher

  1. #include < conio.h >   
  2. #include < string.h >   
  3. #include < stdio.h >   
  4. void main() {  
  5.     char p[50], key[15];  
  6.     int i, n, j;  
  7.     printf("enter your plain text: \n");  
  8.     gets(p);  
  9.     printf("\n");  
  10.     printf("enter your key :\n");  
  11.     printf("\n");  
  12.     gets(key);  
  13.     n = strlen(p);  
  14.     printf("\n");  
  15.     printf("Your cipher text : \n");  
  16.     printf("\n");  
  17.     for (i = 0, j = 0; i < n; i++, j++) {  
  18.         if (p[i] <= 'A' && p[i] >= 'Z') printf("%c", (((p[i] - 'A') + (key[j] - 'A') % 26) + 'A'));  
  19.         if (p[i] <= 'a' && p[i] >= 'z') printf("%c", (((p[i] - 'a') + (key[j] - 'a') % 26) + 'a'));  
  20.         else if (p[i] >= 'A' && p[i] <= 'Z') printf("%c", (((p[i] - 'A') + (key[j] - 'A') % 26) + 'A'));  
  21.         else if (p[i] >= 'a' && p[i] <= 'z') printf("%c", (((p[i] - 'a') + (key[j] - 'a') % 26) + 'a'));  
  22.         else printf("%c", p[i]);  
  23.     }  
  24.     printf("\n");  
  25.     printf("Your Decrypted plain text \n");  
  26.     printf("\n");  
  27.     puts(p);  
  28.     getch();  
  29. }  
Output Window

KEY

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