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:
Updating your Password
Jackson – Decide What Fields Get Serialized/Deserialized
Java Program to Perform Complex Number Multiplication
A Guide to ConcurrentMap
Java Program to Implement Adjacency List
Java Program to Implement Sorted List
Hướng dẫn Java Design Pattern – Mediator
Apache Commons Collections MapUtils
Giới thiệu Google Guice – Binding
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Encode a String to UTF-8 in Java
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Maximum Length Chain of Pairs
Lớp LinkedHashMap trong Java
Guide to Spring @Autowired
Java Program to Implement Suffix Array
Java 8 and Infinite Streams
Format ZonedDateTime to String
Java Program to Implement the One Time Pad Algorithm
Java Program to Implement Meldable Heap
Java Program to add two large numbers using Linked List
Send an email with an attachment
Java Program to Implement TreeSet API
Serve Static Resources with Spring
Introduction to Using FreeMarker in Spring MVC
Constructor Injection in Spring with Lombok
Posting with HttpClient
How to Read HTTP Headers in Spring REST Controllers
Mapping Nested Values with Jackson
4 tính chất của lập trình hướng đối tượng trong Java
Java Program to Implement Hash Tables
Java Program to Implement Find all Cross Edges in a Graph