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 Check Whether an Undirected Graph Contains a Eulerian Path
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Represent Graph Using Adjacency Matrix
What is Thread-Safety and How to Achieve it?
Converting Between Byte Arrays and Hexadecimal Strings in Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Implement Euler Circuit Problem
Template Engines for Spring
Optional trong Java 8
Introduction to Spring Security Expressions
Java Program to Find a Good Feedback Edge Set in a Graph
Sử dụng CyclicBarrier trong Java
Java Program to Implement D-ary-Heap
Introduction to Spring Cloud CLI
Java Program to Implement Hash Tables with Quadratic Probing
Concatenating Strings In Java
Java Program to Search for an Element in a Binary Search Tree
Java Program to Implement Interpolation Search Algorithm
A Guide to the finalize Method in Java
The Thread.join() Method in Java
Bootstrapping Hibernate 5 with Spring
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Java Program to Construct an Expression Tree for an Infix Expression
Spring Boot - Tomcat Port Number
Lớp TreeMap trong Java
Spring Boot Security Auto-Configuration
Guide to Java 8’s Collectors
Retrieve User Information in Spring Security
Java Program to Find the GCD and LCM of two Numbers
Sort a HashMap in Java
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach