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:
How to Replace Many if Statements in Java
Introduction to the Java NIO Selector
Jackson – Unmarshall to Collection/Array
Java – Byte Array to Reader
HashMap trong Java hoạt động như thế nào?
Supplier trong Java 8
Spring @RequestParam Annotation
Java Program to Implement Sieve Of Atkin
Spring Boot - Introduction
Java Program to Implement Hash Tables with Double Hashing
Spring Boot - Batch Service
Thao tác với tập tin và thư mục trong Java
Java Program to Implement Flood Fill Algorithm
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Functional Interfaces in Java 8
An Intro to Spring Cloud Zookeeper
Java Program to Implement Shoelace Algorithm
A Quick Guide to Using Keycloak with Spring Boot
Tổng quan về ngôn ngữ lập trình java
Java Program to implement Sparse Vector
Simultaneous Spring WebClient Calls
Java Web Services – JAX-WS – SOAP
Convert XML to JSON Using Jackson
Show Hibernate/JPA SQL Statements from Spring Boot
Spring Boot - Quick Start
Java Program to Encode a Message Using Playfair Cipher
Java Program to Find the Vertex Connectivity of a Graph
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Spring @Primary Annotation
Java Program to Perform Matrix Multiplication
Java Program to Compute Determinant of a Matrix