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:
Working with Tree Model Nodes in Jackson
Introduction to Spliterator in Java
The DAO with Spring and Hibernate
Debugging Reactive Streams in Java
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Notify User of Login From New Device or Location
Hướng dẫn Java Design Pattern – Command
Spring Web Annotations
Hướng dẫn sử dụng String Format trong Java
Abstract class và Interface trong Java
Java Program to Implement Binomial Heap
Spring Security and OpenID Connect
Java Program to Implement CopyOnWriteArrayList API
HttpClient 4 – Follow Redirects for POST
Java – InputStream to Reader
Injecting Prototype Beans into a Singleton Instance in Spring
JUnit 5 @Test Annotation
Java Program to Find the Connected Components of an UnDirected Graph
Java Program to Implement Sorted Array
A Guide to Java 9 Modularity
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java – InputStream to Reader
Compare Two JSON Objects with Jackson
Java Program to Decode a Message Encoded Using Playfair Cipher
Exception Handling in Java
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Check for balanced parenthesis by using Stacks
Từ khóa static và final trong java
An Example of Load Balancing with Zuul and Eureka
Java Program to Implement the String Search Algorithm for Short Text Sizes
Vòng lặp for, while, do-while trong Java
Java Program to Find the Nearest Neighbor Using K-D Tree Search