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 Sorted Circular Doubly Linked List
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Use rand and srand Functions
Hướng dẫn Java Design Pattern – Visitor
Biến trong java
Lớp Collectors trong Java 8
Spring Boot - Securing Web Applications
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Copy Constructor
Java Program to Implement Bellman-Ford Algorithm
Introduction to Project Reactor Bus
Use Liquibase to Safely Evolve Your Database Schema
Generic Constructors in Java
Introduction to Java Serialization
Comparing Arrays in Java
Java Program to Find the Edge Connectivity of a Graph
Getting Started with Stream Processing with Spring Cloud Data Flow
A Guide to the ResourceBundle
ArrayList trong java
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Implement Adjacency List
Giới thiệu HATEOAS
Quick Guide to @RestClientTest in Spring Boot
Control the Session with Spring Security
Java Web Services – JAX-WS – SOAP
Spring NoSuchBeanDefinitionException
A Guide to the Java ExecutorService
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Mệnh đề if-else trong java
Java Program to Implement LinkedHashMap API
Spring @Primary Annotation