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:
Hướng dẫn Java Design Pattern – MVC
Java Program to Implement Sorted Vector
Spring Boot - Internationalization
How To Serialize and Deserialize Enums with Jackson
Changing Annotation Parameters At Runtime
A Guide to Java 9 Modularity
Immutable Map Implementations in Java
Map to String Conversion in Java
Introduction to Java Serialization
Java Program to Implement Doubly Linked List
How to Iterate Over a Stream With Indices
Using Java Assertions
Request Method Not Supported (405) in Spring
Build a REST API with Spring and Java Config
CyclicBarrier in Java
Command-Line Arguments in Java
Java Program to Check Multiplicability of Two Matrices
Spring Boot - Thymeleaf
“Stream has already been operated upon or closed” Exception in Java
Các nguyên lý thiết kế hướng đối tượng – SOLID
Spring Security Logout
Java Program to Perform Arithmetic Operations on Numbers of Size
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Copy Constructor
How to Get a Name of a Method Being Executed?
How to Change the Default Port in Spring Boot
Spring RestTemplate Request/Response Logging
Java Program to Implement the One Time Pad Algorithm
Java Program to implement Circular Buffer
Java Program to Implement Hash Tables with Double Hashing
OAuth2.0 and Dynamic Client Registration
Custom JUnit 4 Test Runners