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 Uniform-Cost Search
Simple Single Sign-On with Spring Security OAuth2
Spring Boot - Database Handling
Debug a HttpURLConnection problem
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Implement Gaussian Elimination Algorithm
ClassNotFoundException vs NoClassDefFoundError
Converting Strings to Enums in Java
Spring Boot - Logging
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Find a Good Feedback Vertex Set
Batch Processing with Spring Cloud Data Flow
Programmatic Transaction Management in Spring
HttpClient Connection Management
Java Program to Implement the Hill Cypher
Tạo chương trình Java đầu tiên sử dụng Eclipse
Java Program to Implement Disjoint Set Data Structure
Servlet 3 Async Support with Spring MVC and Spring Security
Comparing Objects in Java
Filtering a Stream of Optionals in Java
Spring Boot - Admin Server
Spring Security 5 for Reactive Applications
Java program to Implement Tree Set
SOAP Web service: Authentication trong JAX-WS
Most commonly used String methods in Java
Guide to Spring 5 WebFlux
MyBatis with Spring
Java Program to Implement Miller Rabin Primality Test Algorithm
So sánh ArrayList và Vector trong Java
Lập trình đa luồng với Callable và Future trong Java
Java Program to Implement RenderingHints API
Kiểu dữ liệu Ngày Giờ (Date Time) trong java