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 Sorted Singly Linked List
Java Program to Implement the Hill Cypher
Java program to Implement Tree Set
Java Program to subtract two large numbers using Linked Lists
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Implement Aho-Corasick Algorithm for String Matching
Removing all duplicates from a List in Java
The Difference Between Collection.stream().forEach() and Collection.forEach()
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Deploy a Spring Boot App to Azure
Từ khóa static và final trong java
Mix plain text and HTML content in a mail
Multi Dimensional ArrayList in Java
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Introduction to Spring Boot CLI
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Send email with JavaMail
Java Program to Implement Bloom Filter
Java Program to Implement Graph Structured Stack
Java Program to Perform Left Rotation on a Binary Search Tree
Check If a String Is Numeric in Java
Java Program to implement Priority Queue
A Guide to LinkedHashMap in Java
Spring Boot - Quick Start
Checking for Empty or Blank Strings in Java
Flattening Nested Collections in Java
Functional Interfaces in Java 8
Java Optional as Return Type
Java – File to Reader