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:
Spring Boot - Rest Template
Java Program to Implement Word Wrap Problem
Validate email address exists or not by Java Code
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Find the Connected Components of an UnDirected Graph
Explain about URL and HTTPS protocol
Deque và ArrayDeque trong Java
Model, ModelMap, and ModelAndView in Spring MVC
Exploring the New Spring Cloud Gateway
Java Program to Implement Heap Sort Using Library Functions
Understanding Memory Leaks in Java
Jackson Unmarshalling JSON with Unknown Properties
JUnit 5 for Kotlin Developers
Tạo số và chuỗi ngẫu nhiên trong Java
Java Program to Implement Bubble Sort
Compact Strings in Java 9
Form Validation with AngularJS and Spring MVC
Quick Intro to Spring Cloud Configuration
Java Program to Implement Sorted Vector
LinkedHashSet trong java
wait() and notify() Methods in Java
Serve Static Resources with Spring
@Lookup Annotation in Spring
New Features in Java 14
Java Program to Implement Stein GCD Algorithm
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Java Program to Implement Hash Tree
Java Program to Implement SynchronosQueue API
So sánh HashMap và HashSet trong Java
A Guide to Spring Cloud Netflix – Hystrix
Hướng dẫn Java Design Pattern – Singleton