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:
Adding Parameters to HttpClient Requests
Java Program to Implement Trie
Java Program to Implement Gauss Seidel Method
Guide to WeakHashMap in Java
Java Program to Perform Addition Operation Using Bitwise Operators
Toán tử trong java
Posting with HttpClient
Spring Boot - Database Handling
Java Program to Permute All Letters of an Input String
HttpClient 4 – Follow Redirects for POST
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to Implement TreeSet API
Guide to Character Encoding
Java Program to Implement Variable length array
How to Read HTTP Headers in Spring REST Controllers
Spring Boot - Twilio
Concatenating Strings In Java
String Initialization in Java
Java Program to Implement Nth Root Algorithm
Guide to the Volatile Keyword in Java
Guava Collections Cookbook
Spring Boot Tutorial – Bootstrap a Simple Application
Java Program to Find the Longest Path in a DAG
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Search for an Element in a Binary Search Tree
Java equals() and hashCode() Contracts
Java Program to Perform Search in a BST
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
How to Add a Single Element to a Stream
Introduction to Spring Method Security