Java Program to Implement the Vigenere Cypher

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.

package com.maixuanviet.setandstring;
 
public class VigenereCipher
{
    public static String encrypt(String text, final String key)
    {
        String res = "";
        text = text.toUpperCase();
        for (int i = 0, j = 0; i < text.length(); i++)
        {
            char c = text.charAt(i);
            if (c < 'A' || c > 'Z')
                continue;
            res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }
 
    public static String decrypt(String text, final String key)
    {
        String res = "";
        text = text.toUpperCase();
        for (int i = 0, j = 0; i < text.length(); i++)
        {
            char c = text.charAt(i);
            if (c < 'A' || c > 'Z')
                continue;
            res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }
 
    public static void main(String[] args)
    {
        String key = "VIGENERECIPHER";
        String message = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
        String encryptedMsg = encrypt(message, key);
        System.out.println("String: " + message);
        System.out.println("Encrypted message: " + encryptedMsg);
        System.out.println("Decrypted message: " + decrypt(encryptedMsg, key));
    }
}

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

Related posts:

Java Program to Implement HashMap API
Java Program to Implement Double Order Traversal of a Binary Tree
Guide to java.util.concurrent.Locks
Java Program to Implement Shunting Yard Algorithm
Multipart Upload with HttpClient 4
Using Spring ResponseEntity to Manipulate the HTTP Response
Finding Max/Min of a List or Collection
Phương thức forEach() trong java 8
Add Multiple Items to an Java ArrayList
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java 8 Predicate Chain
Java Program to Find the Connected Components of an UnDirected Graph
Java – Write to File
Fixing 401s with CORS Preflights and Spring Security
Java Program to Implement RenderingHints API
Guava Collections Cookbook
Java Program to Implement Splay Tree
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Spring WebClient vs. RestTemplate
Java Program to Construct an Expression Tree for an Prefix Expression
Java Program to Implement Floyd-Warshall Algorithm
Hướng dẫn Java Design Pattern – Factory Method
A Guide to the ResourceBundle
Spring Boot - Apache Kafka
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Perform the Unique Factorization of a Given Number
The Order of Tests in JUnit
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement Hash Tables Chaining with List Heads
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Implement Trie