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:
Java Program to Implement Merge Sort Algorithm on Linked List
@DynamicUpdate with Spring Data JPA
Hướng dẫn Java Design Pattern – Composite
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement the RSA Algorithm
Java Program to subtract two large numbers using Linked Lists
Transaction Propagation and Isolation in Spring @Transactional
Spring Cloud AWS – S3
Spring 5 and Servlet 4 – The PushBuilder
Guide to Selenium with JUnit / TestNG
Dockerizing a Spring Boot Application
Mockito and JUnit 5 – Using ExtendWith
Java Program to Perform Matrix Multiplication
Java Program to Implement Bit Array
OAuth2 Remember Me with Refresh Token
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Implement Insertion Sort
How to Get the Last Element of a Stream in Java?
Giới thiệu java.io.tmpdir
Java 8 Streams peek() API
LIKE Queries in Spring JPA Repositories
MyBatis with Spring
Tránh lỗi NullPointerException trong Java như thế nào?
Java 14 Record Keyword
ETL with Spring Cloud Data Flow
Java Program to Implement Sorted Singly Linked List
Disable DNS caching
Java Program to Implement Hopcroft Algorithm
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Query Entities by Dates and Times with Spring Data JPA
Annotation trong Java 8