Java Vigenere Cipher Program (Encryption and Decryption)

Here you will get program for vigenere cipher in Java for both encryption and decryption.

Vigenere Cipher is a polyalphabetic substitution technique that is used for encrypting and decrypting a message text.

In this technique we use a table of alphabets A to Z which are written in 26 rows which is also known as Vigenere Table.

Vigenere Cipher

Also Read: Caesar Cipher in Java (Encryption and Decryption)

Also Read: Hill Cipher in Java [Encryption and Decryption]

Encryption

Lets understand encryption by one example.

Plain Text: THEJAVAPROGRAMMER

Key: NEERAJ

We have to generate a new key by repeating above key in cyclic order until its length becomes equal to length of plain text.

New Key: NEERAJNEERAJNEERAJ

Now take first letter of plain text and key, i.e. T and N. See row T and column N in vigenere table, take the letter where row T and column N coincides i.e. G. Here G is the first letter of encrypted or cipher text.

Now take second letter of plain text and key i.e. H and E. Again analyze the table to find second letter of encrypted text. This process is repeated for all the letters in plain text.

So finally we get the encrypted text as.

Cipher Text: GLIAAENTVFGANQQVR

Algebraic Form

We can express above process in algebraic equation in following way.

For plain text (P) and key (K), the cipher text can be obtained as.

E= (P+ Ki) mod 26

Decryption

Let’s understand decryption process with another example.

Cipher Text: GLIAAENTVFGANQQVR

Key: NEERAJ

New Key: NEERAJNEERAJNEERAJ

Take first letter in cipher text and key, i.e. G and N. Now take column N and look for letter G in it, the corrosponding row to letter G is T which is the first letter of plain text after decryption. Repeate the same process for all the letters in cipher text.

Thus the plain text is obtained as.

Plain Text: THEJAVAPROGRAMMER

Algebraic Form

We can express above decryption process in algebraic equation in following way.

For cipher text (E) and key (K), the plain text can be obtained as.

Pi = (Ei – Ki + 26) mod 26

Java Vigenere Cipher Program (Encryption and Decryption)

Below is the implementation of vigenere cipher in java.

Output

Original Message: THEJAVAPROGRAMMER
Key: NEERAJ
Generated Key: NEERAJNEERAJNEERA

Encrypted Message: GLIAAENTVFGANQQVR

Decrypted Message: THEJAVAPROGRAMMER

Comment below if you have any queries related to above program for vigenere cipher in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *