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 Print only Odd Numbered Levels of a Tree
Java Program to Check if a Matrix is Invertible
Debug a HttpURLConnection problem
How To Serialize and Deserialize Enums with Jackson
Collect a Java Stream to an Immutable Collection
Converting Strings to Enums in Java
The Difference Between map() and flatMap()
Hướng dẫn Java Design Pattern – Iterator
Service Registration with Eureka
Spring – Injecting Collections
Spring Cloud AWS – RDS
A Guide to HashSet in Java
Java Program to Encode a Message Using Playfair Cipher
Java Program to Represent Graph Using Adjacency List
Tìm hiểu về Web Service
Spring Security – Reset Your Password
Java Program to Permute All Letters of an Input String
Java Program to Implement Uniform-Cost Search
A Guide To UDP In Java
Java Program to Implement Stack API
Uploading MultipartFile with Spring RestTemplate
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java List UnsupportedOperationException
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Giới thiệu java.io.tmpdir
Java Program to Implement Sparse Matrix
Spring Boot Configuration with Jasypt
Spring MVC Custom Validation
Intro to Spring Boot Starters
Java Program to Create a Random Linear Extension for a DAG
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Generate Random Numbers Using Probability Distribution Function