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:
JUnit5 @RunWith
Java Program to Implement the Program Used in grep/egrep/fgrep
Spring Data JPA and Null Parameters
Java Program to Implement Quick Sort with Given Complexity Constraint
Spring Boot - Servlet Filter
Simplify the DAO with Spring and Java Generics
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement Pagoda
Java Program to Perform integer Partition for a Specific Case
Custom HTTP Header with the HttpClient
Java Program to Use rand and srand Functions
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
How to Manually Authenticate User with Spring Security
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
How to Round a Number to N Decimal Places in Java
Java Program to Implement Attribute API
Java Program to Generate a Random Subset by Coin Flipping
A Guide to Java HashMap
Java Program to Permute All Letters of an Input String
Java Program to Implement Affine Cipher
The Guide to RestTemplate
Implementing a Binary Tree in Java
An Intro to Spring Cloud Security
Mapping Nested Values with Jackson
Spring Boot Annotations
Basic Authentication with the RestTemplate
Java Program to Implement Control Table
Disable Spring Data Auto Configuration