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:
Ways to Iterate Over a List in Java
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Lập trình đa luồng với CompletableFuture trong Java 8
Create a Custom Auto-Configuration with Spring Boot
Transactions with Spring and JPA
Java Program to Implement Cartesian Tree
Java Program to Implement ConcurrentLinkedQueue API
Java Program to Implement Sorted Circular Doubly Linked List
Extra Login Fields with Spring Security
Java Program to Perform LU Decomposition of any Matrix
Java Program to Generate Random Numbers Using Probability Distribution Function
Allow user:password in URL
Cài đặt và sử dụng Swagger UI
Java Program to Find Path Between Two Nodes in a Graph
Object cloning trong java
Create a Custom Exception in Java
String Operations with Java Streams
Send an email with an attachment
Spring Boot - Securing Web Applications
Hướng dẫn Java Design Pattern – Factory Method
Split a String in Java
Composition, Aggregation, and Association in Java
Registration with Spring Security – Password Encoding
Flattening Nested Collections in Java
Injecting Prototype Beans into a Singleton Instance in Spring
Java Program to Implement ArrayDeque API
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to Implement Threaded Binary Tree
Java Program to Implement Heap
Java Program to Construct K-D Tree for 2 Dimensional Data
Consuming RESTful Web Services
Deque và ArrayDeque trong Java