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:

Guide to Spring 5 WebFlux
Giới thiệu SOAP UI và thực hiện test Web Service
Comparing Dates in Java
Getting Started with Stream Processing with Spring Cloud Data Flow
How to Get All Dates Between Two Dates?
Immutable ArrayList in Java
Java Program to Implement Sorted Circular Doubly Linked List
Derived Query Methods in Spring Data JPA Repositories
Java Program to Implement Sorted Circularly Singly Linked List
Introduction to the Java NIO Selector
Java Program to Implement Bloom Filter
Introduction to Java 8 Streams
Java Program to Implement Sorted List
Java Program to find the peak element of an array using Binary Search approach
Java Program to Perform Arithmetic Operations on Numbers of Size
Spring Security OAuth2 – Simple Token Revocation
Abstract class và Interface trong Java
Working with Tree Model Nodes in Jackson
Java Program for Topological Sorting in Graphs
Introduction to Using FreeMarker in Spring MVC
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Solve a Matching Problem for a Given Specific Case
Một số nguyên tắc, định luật trong lập trình
Spring Boot - Zuul Proxy Server and Routing
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Introduction to Spring Cloud CLI
Convert XML to JSON Using Jackson
The StackOverflowError in Java
New Features in Java 14
How to Kill a Java Thread
Configure a RestTemplate with RestTemplateBuilder
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x