Vigenere Cipher Program in Java with Output

In this tutorial, I will be sharing the Vigenere Cipher program in java with output for encryption and decryption. A vigenere cipher program is a form of polyalphabetic substitution. Before moving further let's understand what is Vigenere cipher.

Read Also: Caesar Cipher in Java

What is Vigenere Cipher?

Vigenere cipher is used to encrypt the alphabetic text by using a series of different Caesar ciphers, based on the letters of a keyword.

In Vigenere cipher, to encrypt the plaintext we use a Vigenere table or Vigenere square. Vigenere table consists of the alphabet from A to Z written out 26 times in different rows, each alphabet shifted cyclically to the left compared to the previous alphabet, leads to the 26 possible Caesar ciphers as shown in the diagram below.


Vigenere Cipher Program in Java

   Above Image is in Public Domain

For Example:

The plaintext is the message to be encrypted.

Encryption:

Plaintext:  JAVAHUNGRYBLOG

The person sending the message picks a keyword and repeat it continuously until it matches the length of the plaintext. You can pick any word as a keyword which consists of alphabets only.

Suppose keyword is LEMON

Plaintext:    JAVAHUNGRYBLOG        
Key:            LEMONLEMONLEMO

1. Pick the first letter of key and plaintext, i.e 'L' and 'J' respectively. Check row L and column J in the vigenere table (i.e above table). Pick the letter from the vigenere table where row L and column J coincides i.e 'U'. Hence, 'U' is the first letter of the ciphertext or encrypted text.

Letter at the intersection of [key-row, plaintext-col] is the enciphered letter.

2. Now take the second letter of key and plaintext, i.e 'E' and 'A' respectively. Check row E and column A in the vigenere table. Pick the letter from the vigenere table where row E and column A coincides i.e 'E'. Hence, 'E' is the second letter of the ciphertext or encrypted text.

The above procedure is repeated for all the letters in plaintext.

Finally, we will obtain the following ciphertext (encrypted text):

Ciphertext:  UEHOUFRSFLMPAU

Decryption:

Decryption is performed by going to the row in vigenere table corresponding to the key, searching the position of ciphertext letter in that row and then using the column's label as the plain text.

The decryption process example is given below.

Ciphertext: UEHOUFRSFLMPAU
Key:            LEMONLEMONLEMO

1. Pick the first letter of key and ciphertext, i.e 'L' and 'U' respectively. Now, go to row L and look for the letter 'U' in it, the corresponding column to letter U is J which is the first letter of the plaintext.

2. Pick the second letter of key and ciphertext i.e 'E' and 'E' respectively. Now, go to row E and look for the letter 'E' in it, the corresponding column to letter E is A which is the second letter of the plaintext.
 
The above procedure is repeated for all the letters in the ciphertext.

Finally, we will obtain the following plaintext(original message):

Plaintext: JAVAHUNGRYBLOG

Algebraic Description:


Encryption:

According to Wikipedia, we can express the encryption of vigenere cipher in an algebraic equation in the following way.

For key K and plaintext P, the ciphertext C can be obtained by using the below equation:

C= (P+ Ki) mod 26

Decryption:

According to Wikipedia, we can express the decryption of vigenere cipher in an algebraic equation in the following way.

For key K and ciphertext C, the plaintext P can be obtained by using the below equation:

Pi = (Ci – Ki + 26) mod 26
   

Vigenere Cipher Program in Java with Output for Encryption and Decryption


public class VigenereCipher
{
    
    public static void main(String arg[])
    {
 String plaintext = "JAVAHUNGRYBLOG";
 String keyword = "LEMON";
 encryptDecrypt(plaintext,keyword);
    }

    
    public static void encryptDecrypt(String plaintext, String keyword)
    {
 //Converting plaintext to char array
 char msg[] = plaintext.toCharArray();
 int msgLen = msg.length;
 int i,j;
  
 // Creating new char arrays
 char key[] = new char[msgLen];
 char encryptedMsg[] = new char[msgLen];
 char decryptedMsg[] = new char[msgLen];
  
 /* generate key, using keyword in cyclic 
 manner equal to the length of 
 original message i.e plaintext */
 for(i = 0, j = 0; i < msgLen; ++i, ++j)
 {
  if(j == keyword.length())
  {
   j = 0;
  }
  key[i] = keyword.charAt(j);
 }
  
 //encryption code 
 for(i = 0; i < msgLen; ++i)
  encryptedMsg[i] = (char) (((msg[i] + key[i]) % 26) + 'A');
 
 //decryption code
 for(i = 0; i < msgLen; ++i)
  decryptedMsg[i] = (char)((((encryptedMsg[i] - key[i]) + 26) % 26) + 'A');
  
 System.out.println("Original Message: " + plaintext);  
 System.out.println("Keyword: " + keyword);
 /* String.valueOf() method converts
    char[] to String */
 System.out.println("Key: " + String.valueOf(key));
 System.out.println();
 System.out.println("Encrypted Message: " + String.valueOf(encryptedMsg));
 System.out.println();        
 System.out.println("Decrypted Message: " + String.valueOf(decryptedMsg));
   }
}

Output:

Original Message: JAVAHUNGRYBLOG
Keyword: LEMON
Key: LEMONLEMONLEMO

Encrypted Message: UEHOUFRSFLMPAU

Decrypted Message: JAVAHUNGRYBLOG


That's all for the day, please mention in comments if you have any questions related to vigenere cipher program in java with output for encryption and decryption.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry