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 Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
New Stream Collectors in Java 9
Java Program to Implement Treap
Pagination and Sorting using Spring Data JPA
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Implement Sieve Of Atkin
Hướng dẫn sử dụng Java Reflection
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Spring Cloud Connectors and Heroku
Java Program to Implement Sorted Circularly Singly Linked List
Automatic Property Expansion with Spring Boot
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
How to Kill a Java Thread
Bootstrapping Hibernate 5 with Spring
Reversing a Linked List in Java
Prevent Cross-Site Scripting (XSS) in a Spring Application
Introduction to the Functional Web Framework in Spring 5
Integer Constant Pool trong Java
Spring Boot - Twilio
Convert XML to JSON Using Jackson
Mapping Nested Values with Jackson
Connect through a Proxy
Spring Boot - Bootstrapping
Java Program to Implement Patricia Trie
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Hashing a Password in Java
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java 8 Stream findFirst() vs. findAny()
Java Program to Construct K-D Tree for 2 Dimensional Data
A Guide to Java HashMap