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:
Functional Interfaces in Java 8
Introduction to the Java NIO2 File API
A Guide to JUnit 5 Extensions
Java Program to Implement Interpolation Search Algorithm
Spring WebClient and OAuth2 Support
Hướng dẫn Java Design Pattern – Object Pool
How To Serialize and Deserialize Enums with Jackson
Mix plain text and HTML content in a mail
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Guide to java.util.concurrent.BlockingQueue
Spring Cloud AWS – RDS
Luồng Daemon (Daemon Thread) trong Java
Spring Security 5 – OAuth2 Login
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Spring Boot - Tomcat Port Number
Java Program to Implement Cubic convergence 1/pi Algorithm
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Represent Linear Equations in Matrix Form
Spring Cloud – Tracing Services with Zipkin
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Registration – Password Strength and Rules
Vector trong Java
Guide to Spring 5 WebFlux
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement LinkedHashSet API
Java Program to Implement TreeSet API
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Tính trừu tượng (Abstraction) trong Java
Checked and Unchecked Exceptions in Java
Java Program to Construct an Expression Tree for an Postfix Expression
LIKE Queries in Spring JPA Repositories