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:
Spring JDBC
Java Program to Perform Right Rotation on a Binary Search Tree
Convert Character Array to String in Java
Java Program to Implement Sorted Circular Doubly Linked List
Sort a HashMap in Java
A Guide to JPA with Spring
The “final” Keyword in Java
Java Program to Implement LinkedBlockingDeque API
Jackson – Unmarshall to Collection/Array
So sánh Array và ArrayList trong Java
Guava CharMatcher
The Thread.join() Method in Java
Vector trong Java
Java Program to Implement Unrolled Linked List
Working with Network Interfaces in Java
Java Program to Implement Red Black Tree
Inject Parameters into JUnit Jupiter Unit Tests
Merging Streams in Java
How to Use if/else Logic in Java 8 Streams
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Implement Binary Heap
Java Program to Construct an Expression Tree for an Prefix Expression
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java equals() and hashCode() Contracts
Marker Interface trong Java
Spring Security Login Page with React
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to find the maximum subarray sum using Binary Search approach
Spring 5 and Servlet 4 – The PushBuilder
Java Program to Implement Find all Back Edges in a Graph
DistinctBy in the Java Stream API