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 Solve a Matching Problem for a Given Specific Case
Java Program to Generate Random Numbers Using Probability Distribution Function
A Guide to Concurrent Queues in Java
Filtering a Stream of Optionals in Java
How to Read HTTP Headers in Spring REST Controllers
Spring Data MongoDB – Indexes, Annotations and Converters
Java Program to Implement the Bin Packing Algorithm
Adding Parameters to HttpClient Requests
Java Program to Implement the MD5 Algorithm
Java – Combine Multiple Collections
Spring @RequestMapping New Shortcut Annotations
Introduction to Thread Pools in Java
Java Switch Statement
Comparing Strings in Java
Hướng dẫn Java Design Pattern – Composite
Java Program to Implement Fenwick Tree
Java Program to Implement the Monoalphabetic Cypher
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Introduction to Spring MVC HandlerInterceptor
Spring Boot - Securing Web Applications
Spring Boot - Exception Handling
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Spring MVC + Thymeleaf 3.0: New Features
Java Program to Implement Skip List
Java Timer
Mapping Nested Values with Jackson
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Giới thiệu Google Guice – Binding
Hướng dẫn Java Design Pattern – Flyweight
An Introduction to Java.util.Hashtable Class
Shuffling Collections In Java
Java Program to Implement Floyd-Warshall Algorithm