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:
Send email with authentication
Tính đóng gói (Encapsulation) trong java
Encode/Decode to/from Base64
Concatenating Strings In Java
Concrete Class in Java
Comparing Long Values in Java
Introduction to Java Serialization
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Find Transitive Closure of a Graph
Java Program to Describe the Representation of Graph using Incidence Matrix
Java Program to add two large numbers using Linked List
Reversing a Linked List in Java
Spring WebClient Requests with Parameters
Java Program to Implement Binary Search Tree
Posting with HttpClient
Write/Read cookies using HTTP and Read a file from the internet
Java Streams vs Vavr Streams
Compare Two JSON Objects with Jackson
A Guide to the ViewResolver in Spring MVC
Guide to java.util.Formatter
Spring @RequestMapping New Shortcut Annotations
Java Program to Implement Selection Sort
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to Perform Stooge Sort
Java Optional as Return Type
Một số từ khóa trong Java
Introduction to Spring Data REST
Bootstrapping Hibernate 5 with Spring
Lớp Collections trong Java (Collections Utility Class)
Toán tử instanceof trong java
String Joiner trong Java 8