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 NIO2 Path API
A Guide to @RepeatedTest in Junit 5
Java Program to implement Dynamic Array
An Introduction to Java.util.Hashtable Class
Spring 5 WebClient
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
How to Change the Default Port in Spring Boot
Java Program to Implement AA Tree
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Check Whether Graph is DAG
A Guide to LinkedHashMap in Java
Spring Data JPA @Query
Guide to the Java ArrayList
Java – Delete a File
@DynamicUpdate with Spring Data JPA
Java Program to Implement LinkedTransferQueue API
Java Program to Implement Sorted Vector
Send an email with an attachment
Spring Security Basic Authentication
Java Program to Generate Random Numbers Using Multiply with Carry Method
Luồng Daemon (Daemon Thread) trong Java
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Implement CopyOnWriteArrayList API
Explain about URL and HTTPS protocol
Converting Strings to Enums in Java
Java Program to Implement Brent Cycle Algorithm
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Implement Control Table
Creating a Web Application with Spring 5
Spring Security Registration – Resend Verification Email