Java Program to Implement the Vigenere Cipher

This is a java program to implement Vigenere cipher. The Vigenère cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution.

Here is the source code of the Java Program to Implement the Vigenere Cypher. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. public class VigenereCipher
  5. {
  6.     public static String encrypt(String text, final String key)
  7.     {
  8.         String res = "";
  9.         text = text.toUpperCase();
  10.         for (int i = 0, j = 0; i < text.length(); i++)
  11.         {
  12.             char c = text.charAt(i);
  13.             if (c < 'A' || c > 'Z')
  14.                 continue;
  15.             res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
  16.             j = ++j % key.length();
  17.         }
  18.         return res;
  19.     }
  20.  
  21.     public static String decrypt(String text, final String key)
  22.     {
  23.         String res = "";
  24.         text = text.toUpperCase();
  25.         for (int i = 0, j = 0; i < text.length(); i++)
  26.         {
  27.             char c = text.charAt(i);
  28.             if (c < 'A' || c > 'Z')
  29.                 continue;
  30.             res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
  31.             j = ++j % key.length();
  32.         }
  33.         return res;
  34.     }
  35.  
  36.     public static void main(String[] args)
  37.     {
  38.         String key = "VIGENERECIPHER";
  39.         String message = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
  40.         String encryptedMsg = encrypt(message, key);
  41.         System.out.println("String: " + message);
  42.         System.out.println("Encrypted message: " + encryptedMsg);
  43.         System.out.println("Decrypted message: " + decrypt(encryptedMsg, key));
  44.     }
  45. }

Output:

$ javac VigenereCipher.java
$ java VigenereCipher
 
String: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted message: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted message: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.