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 Perform integer Partition for a Specific Case
Send email with JavaMail
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Implement Hash Tables Chaining with List Heads
A Quick Guide to Using Keycloak with Spring Boot
Convert Hex to ASCII in Java
Java Optional as Return Type
Java Program to Describe the Representation of Graph using Adjacency Matrix
Java – Reader to Byte Array
Phương thức tham chiếu trong Java 8 – Method References
Inheritance with Jackson
Working with Tree Model Nodes in Jackson
Summing Numbers with Java Streams
Java Program to Implement Ternary Heap
Extract network card address
Spring Data JPA @Query
Hướng dẫn Java Design Pattern – MVC
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Life Cycle of a Thread in Java
Immutable Map Implementations in Java
Spring Security with Maven
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
More Jackson Annotations
Java Program to Implement Threaded Binary Tree
Giới thiệu HATEOAS
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Perform Matrix Multiplication
Jackson Unmarshalling JSON with Unknown Properties
Spring Security Custom AuthenticationFailureHandler
Java Program to Perform Left Rotation on a Binary Search Tree
Spring Security Form Login