# 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.

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.

```public class VigenereCipherJava {
public static void main(String...s){
char msg[] = {'T','H','E','J','A','V','A','P','R','O','G','R','A','M','M','E','R'};
char key[] = {'N','E','E','R','A','J'};
int msgLen = msg.length, i, j;

char newKey[] = new char[msgLen];
char encryptedMsg[] = new char[msgLen];
char decryptedMsg[] = new char[msgLen];

//generate new key in cyclic manner equal to the length of original message
for(i = 0, j = 0; i < msgLen; ++i, ++j){
if(j == key.length)
j = 0;

newKey[i] = key[j];
}

//encryption
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = (char)(((msg[i] + newKey[i]) % 26) + 'A');

//decryption
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (char)((((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A');

System.out.println("Original Message: " + String.valueOf(msg));		//String.valueOf() converts character array to String
System.out.println("Key: " + String.valueOf(key));
System.out.println("Generated Key: " + String.valueOf(newKey));
System.out.println("\nEncrypted Message: " + String.valueOf(encryptedMsg));
System.out.println("\nDecrypted Message: " + String.valueOf(decryptedMsg));
}
}```

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.