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 Implement Sieve Of Sundaram
Guide to @JsonFormat in Jackson
CyclicBarrier in Java
Registration – Activate a New Account by Email
Posting with HttpClient
Adding Parameters to HttpClient Requests
Java Program to Implement Hash Tables with Double Hashing
Java Program to Implement IdentityHashMap API
Functional Interfaces in Java 8
Java Program to Construct an Expression Tree for an Prefix Expression
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Validate email address exists or not by Java Code
Java Program to Implement Fenwick Tree
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Java TreeMap vs HashMap
Java Program to Solve the Fractional Knapsack Problem
Java Program to Implement Selection Sort
Updating your Password
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Program to Implement Segment Tree
Lập trình đa luồng trong Java (Java Multi-threading)
File Upload with Spring MVC
Hướng dẫn Java Design Pattern – Bridge
HttpClient 4 – Follow Redirects for POST
Java InputStream to Byte Array and ByteBuffer
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Spring 5 Functional Bean Registration
How to Read HTTP Headers in Spring REST Controllers