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 – DAO
Java Program to Check Whether a Given Point is in a Given Polygon
Jackson – Change Name of Field
Giới thiệu Java 8
Java Program to Implement TreeMap API
Lập trình đa luồng với Callable và Future trong Java
Java Program to Implement K Way Merge Algorithm
Spring Boot - Building RESTful Web Services
Get and Post Lists of Objects with RestTemplate
New Features in Java 11
Java Program to Implement RenderingHints API
Introduction to Netflix Archaius with Spring Cloud
Tiêu chuẩn coding trong Java (Coding Standards)
Functional Interface trong Java 8
Java Program to Solve any Linear Equations
Spring Boot - Flyway Database
Java Program to implement Circular Buffer
The Difference Between map() and flatMap()
Java Program to Implement Quick Sort Using Randomization
Overview of the java.util.concurrent
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Adding Shutdown Hooks for JVM Applications
Quick Guide to Spring Bean Scopes
Functional Interfaces in Java 8
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Refactoring Design Pattern với tính năng mới trong Java 8
A Guide to the finalize Method in Java
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Lập trình mạng với java
Creating a Web Application with Spring 5
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Generate Random Numbers Using Multiply with Carry Method