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 Find All Pairs Shortest Path
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Introduction to the Java ArrayDeque
Setting Up Swagger 2 with a Spring REST API
Hướng dẫn Java Design Pattern – Dependency Injection
Java Streams vs Vavr Streams
Java Program to Represent Graph Using Incidence List
Java Program to implement Dynamic Array
Java Program to Check Whether a Given Point is in a Given Polygon
Java Program to Implement Insertion Sort
Java – Convert File to InputStream
Understanding Memory Leaks in Java
Spring Boot - Unit Test Cases
Spring Cloud – Tracing Services with Zipkin
How to Iterate Over a Stream With Indices
Introduction to Liquibase Rollback
Java Program to Implement Quick Sort Using Randomization
Java Program to Implement Red Black Tree
Simplify the DAO with Spring and Java Generics
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
How to use the Spring FactoryBean?
Spring Boot - OAuth2 with JWT
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Introduction to Eclipse Collections
Date Time trong Java 8
Spring Security Logout
XML Serialization and Deserialization with Jackson
Java Program to Implement Stack
List Interface trong Java
Java Program to Implement PriorityQueue API