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 Optimize Wire Length in Electrical Circuit
Prevent Brute Force Authentication Attempts with Spring Security
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Apache Tiles Integration with Spring MVC
Using a Spring Cloud App Starter
Java Program to Implement the Hill Cypher
So sánh ArrayList và LinkedList trong Java
Spring Data Reactive Repositories with MongoDB
Java – Write an InputStream to a File
Lớp HashMap trong Java
Simplify the DAO with Spring and Java Generics
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java 14 Record Keyword
Spring @Primary Annotation
A Custom Data Binder in Spring MVC
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Cachable Static Assets with Spring MVC
Java Program to Implement Hopcroft Algorithm
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
A Guide to ConcurrentMap
Concrete Class in Java
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Jackson – Unmarshall to Collection/Array
Java Program to Implement Karatsuba Multiplication Algorithm
Guide to Java 8 groupingBy Collector
Java Program to implement Bit Matrix
RegEx for matching Date Pattern in Java
Java Program to Implement Sparse Array
Posting with HttpClient
Lớp Arrarys trong Java (Arrays Utility Class)
Java Program to Implement Borwein Algorithm