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:
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Hamiltonian Cycle Algorithm
Enum trong java
Ways to Iterate Over a List in Java
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Check whether Graph is a Bipartite using BFS
Một số từ khóa trong Java
Java Program to Perform Complex Number Multiplication
4 tính chất của lập trình hướng đối tượng trong Java
Java Program to Implement Adjacency Matrix
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement Interval Tree
Serverless Functions with Spring Cloud Function
Guava – Join and Split Collections
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Introduction to Spring MVC HandlerInterceptor
Java Program to Implement Ternary Heap
An Introduction to Java.util.Hashtable Class
MyBatis with Spring
Apache Commons Collections Bag
Java Program to Check Cycle in a Graph using Graph traversal
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Creating a Custom Starter with Spring Boot
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Program to Perform Search in a BST
Allow user:password in URL
Check if there is mail waiting
Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Implement Quick sort
Spring @RequestParam Annotation
Java Program to Implement Bubble Sort