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:
String Joiner trong Java 8
Java – String to Reader
Introduction to the Java NIO Selector
Java Program to Implement String Matching Using Vectors
Introduction to the Java ArrayDeque
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
The Thread.join() Method in Java
Lớp TreeMap trong Java
Simple Single Sign-On with Spring Security OAuth2
Java Optional as Return Type
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Guide to Guava Multimap
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java – Write a Reader to File
Java Program to Implement Circular Singly Linked List
Java Program to Implement ArrayList API
Abstract class và Interface trong Java
Java Program to Implement Cartesian Tree
Retrieve User Information in Spring Security
Hướng dẫn Java Design Pattern – Composite
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
A Guide to LinkedHashMap in Java
Instance Profile Credentials using Spring Cloud
Spring JDBC
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Simplify the DAO with Spring and Java Generics
Java Program to Implement Rolling Hash
Spring Boot - Cloud Configuration Client
Receive email using IMAP
Java Program to Find the Minimum value of Binary Search Tree