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:
Introduction to Netflix Archaius with Spring Cloud
Java 14 Record Keyword
Spring Security Registration – Resend Verification Email
Java Program to Implement Find all Back Edges in a Graph
Supplier trong Java 8
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Spring 5 Testing with @EnabledIf Annotation
Converting Between Byte Arrays and Hexadecimal Strings in Java
Spring Boot - Rest Controller Unit Test
Introduction to Spring Cloud OpenFeign
Introduction to Spring Data JPA
Java Program to Find a Good Feedback Vertex Set
Java Program to Perform Sorting Using B-Tree
Từ khóa this và super trong Java
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Hướng dẫn Java Design Pattern – Template Method
Spring Boot - Tomcat Deployment
Converting Strings to Enums in Java
Arrays.asList vs new ArrayList(Arrays.asList())
JPA/Hibernate Persistence Context
Giới thiệu Design Patterns
Java Program to Implement Min Heap
Các kiểu dữ liệu trong java
Java Program to Implement the Monoalphabetic Cypher
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Introduction to Liquibase Rollback
Introduction to the Java NIO2 File API
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Java Program to Implement Lloyd’s Algorithm
Java Program to Implement Hamiltonian Cycle Algorithm