Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Cryptography
3.
Vigenere Cipher
4.
Implementation
4.1.
Program
4.2.
Complexities
5.
Frequently Asked Questions
5.1.
What is Non-monoalphabetic?
5.2.
What are the examples of Non-monoalphabetic ciphers?
5.3.
Vigener Cipher is based on what Cipher?
5.4.
What should be the length of a key in Vigener Cipher?
5.5.
What if the length of the key is different from the plaintext?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Non-monoalphabetic Vigenere Cipher

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This blog will discuss the Non-monoalphabetic Vignere Cipher or Polyaplhabetic Vigenere CIpher. Throughout this blog, we will be addressing the Vigenere Cipher as Non-monoalphabetic. We will also learn how to implement a Vigenere Cipher and discuss its logic.

 

Non-monoalphabetic: Vigenere Cipher

But before that, let's talk about cryptography in brief.

Cryptography

Cryptography is one of the subjects in computer science that focus on secure communication between the sender and receiver and also discusses the methods we can use to secure digital data like digital signature uploaded on the internet.

With cryptography, we do not need to worry about the confidentiality and integrity of the data during transmission.
 

You need to know a few terms we will frequently use during the explanation.

  • Plaintext: The standard text we will convert into a non-readable format.
     
  • Ciphertext: The text we will generate after converting the plain text by applying cryptography techniques.
     
  • Key: A key is a string of characters or bits that helps us convert plain text into cipher text and is only known to the sender and receiver.
     
  • Encryption: Encryption converts plain text into cipher text with the help of a secret key.
     
  • Decryption: Decryption converts cipher text into standard or plain text with the help of a secret key.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Vigenere Cipher

Let's discuss the term Non-monoalphabetic and Monoalphabetic. In Monoalphabetic Ciphers, each character of the plaintext is mapped to a unique character in Cipher text. Shift Cipher and Substitution Cipher are the two examples of Monoalphabetic Cipher.

Non-Monoalphabetic Ciphers are almost similar to Monoaplhabetic Ciphers, but In Non-Monoalphabetic Ciphers, the plaintext character is mapped with the multiple characters in a Cipher text. Non-Monoalphabetic Ciphers are based on the substitution Cipher, but it follows the one-to-many relationship between the plaintext character and Cipher text characters.

Vigenere Cipher is an example of non-monoalphabetic Ciphers. Vigenere Cipher is named after Blaise de Vigenere, who was a French Cryptographer. Originally Vigenere Ciphers was founded by  Giovan Battista Bellaso, but Blaise de Vigenere was the one who studied it and developed the final result.

Vigener Cipher is an algorithm that encrypts and decrypts the alphabetic characters of plaintext and cipher text based on the polyalphabetic substitution cipher. Before we discuss an example of a Vigener Cipher, there are a few points to remember. 

  • You must have a Vigener table or Vigener square. A Vigener table is a 26*26 matrix filled with alphabets from A-Z.
     
  • The first row in the Vigener square represents the plaintext characters, and 1st column represents the key characters.
 Vigener table
  • The length of the Key for encryption and decryption should be the same as the plaintext, and if it is not, then we will repeat the characters of the Key in sequence until it is of the same length.

Example

PlainText = PLEASE HELP ME

Key = NINJA

As you can see, the key is not the same length as the plaintext, so we will repeat it in a circular sequence until it is the same as the PlainText. 

Key = NINJANINJANI
 

PlainText = PLEASE HELP ME

Key =          NINJAN  INJA    NI

Encryption

Now will see the Vigener table and observe the interaction point of the characters of plaintext and key.

For example, the character ‘P’ from the plaintext and ‘N’ from the key intersects with the character ‘C’ in Vigener Cipher, so this will be our Ciphertext character.

 Vigener table

 

The same goes for the character ‘L’ from Plaintext, and ‘I’ from key intersects at the character ‘T.’

This process will be repeated for the remaining characters until we have the Cipher text.

CipherText = CTRJSR PRUP ZM

Decryption

To decrypt the Cipher text, you need to check the rows of the Vigener table. Rows in the Vigener Cipher refer to the Key. In the rows, find out the position of the Ciphertext character and observe the corresponding character in the plaintext column. 

 

cipher text
 Vigener table

After repeating the process for all the remaining characters, our plaintext will be PLEASE HELP ME.

Implementation

Now that you have understood the Vigener Cipher let's discuss the implementation of the Vigener Cipher. For implementation, we will be using the C++ programming language. You can use any language. It's up to you.

Before we see the implementation, remember that our approach will differ from the above example.

  • According to the table below, you need to convert the characters of plaintext and key into the corresponding numeric form.
     
  • Also, to perform encryption, we will use the formula Cipher[i] = (Plaintext[i] + Key[i])mod26, and for decryption, Plaintext[i] = (Ciphertext[i] - Key[i])mod26.
     
  • We will also implement a key generator method to check if the length of the key is equal to plaintext; if it is not, then we will make it equal.
     
  • The following converts the capital characters into ciphertext characters.

Program

#include<bits/stdc++.h>

using namespace std;

/* Encryption function to generate the cipher text*/
string Encryption(string plaintext, string newkey) {
  string ciphertext;

  for (int i = 0; i < plaintext.size(); i++) {
    /* performing the Encryption*/
    char character = ((plaintext[i] + newkey[i]) % 26);

    /*converting the character using the ASCII*/
    character += 'A';

    ciphertext.push_back(character);
  }
  cout << "Cipher Text: " << ciphertext << endl;
  return ciphertext;
}

/* Decryption function to generate the plain text*/
void Decryption(string ciphertext, string newkey) {
  string message;

  for (int i = 0; i < ciphertext.size(); i++) {
    /* performing the Decryption*/
    char character = (ciphertext[i] - newkey[i] + 26) % 26;

    /*converting the character using the ASCII*/
    character += 'A';

    message.push_back(character);
  }


  cout << "Original Message: " << message;
}

/* key generator function to make the key equal to the length of plaintext*/
string Keygenerator(string plaintext, string key) {
  int x = plaintext.size();

  /* comparing the size of key and plaintext*/
  if (key.size() == plaintext.size()) {
    return key;
  }

  /* pushing the character back to make the size equal*/
  for (int i = 0; i < plaintext.size(); i++) {
    key.push_back(key[i]);
  }
  return key;
}

int main() {
  string plaintext;
  string key;

  cout << "Enter the PlainText: ";
  cin >> plaintext;

  cout << "Enter the Key: ";
  cin >> key;

  string newkey = Keygenerator(plaintext, key);

  string ciphertext = Encryption(plaintext, newkey);

  Decryption(ciphertext, newkey);
  return 0;
}

Output

Output

Complexities

Time Complexity - The time complexity of the above program is O(n), where n is the length of the plaintext.

Space Complexity - The space complexity of the above program is also O(n), where n is the length of the plaintext.

Frequently Asked Questions

What is Non-monoalphabetic?

In Non-Monoalphabetic Ciphers, the plaintext character is mapped with the multiple characters in a Cipher text.

What are the examples of Non-monoalphabetic ciphers?

Playfair, Vigener. Hill and one-time pad are examples of Non-monoalphabetic Ciphers.

Vigener Cipher is based on what Cipher?

Vigener Cipher is based on the substitution cipher.

What should be the length of a key in Vigener Cipher?

The length of the key should be the same as the length of the plaintext.

What if the length of the key is different from the plaintext?

Then you must use the key generator to make the length of the key as same as the plaintext.

Conclusion

This article discussed the Non-monoalphabetic Vigener Cipher and the terms Monolaphabetic and Non-monoalphabetic. We also discussed an example to demonstrate the Vigener Cipher. We have implemented the Vigener Cipher in C++.

To learn more about cryptography, check out the following articles.

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Previous article
Special thy Affine Cipher
Next article
Polyalphabetic: Hill Cipher
Live masterclass