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:

Guava CharMatcher
Debug a JavaMail Program
A Guide to BitSet in Java
Lớp Properties trong java
Introduction to Spring Data JPA
Java Program to Implement Shoelace Algorithm
Java Program to Implement Fermat Factorization Algorithm
A Guide To UDP In Java
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to Perform Complex Number Multiplication
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Iterable to Stream in Java
Java Program to Implement Selection Sort
Request Method Not Supported (405) in Spring
An Intro to Spring Cloud Zookeeper
Introduction to the Java ArrayDeque
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Create a Balanced Binary Tree of the Incoming Data
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to find the peak element of an array using Binary Search approach
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Chuyển đổi từ HashMap sang ArrayList
Intro to Spring Boot Starters
Java – Generate Random String
Java Program to Implement Circular Doubly Linked List
Java Program to Implement Rope
Feign – Tạo ứng dụng Java RESTful Client
Spring Autowiring of Generic Types