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 Sort an Array of 10 Elements Using Heap Sort Algorithm
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Implement Double Ended Queue
New Features in Java 10
Spring Cloud AWS – Messaging Support
Java 9 Stream API Improvements
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Implement Self organizing List
Add Multiple Items to an Java ArrayList
Spring Webflux and CORS
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Hướng dẫn Java Design Pattern – Mediator
Đồng bộ hóa các luồng trong Java
Java IO vs NIO
Introduction to Project Reactor Bus
Spring Boot - Quick Start
Spring 5 WebClient
HttpClient 4 – Follow Redirects for POST
Apache Commons Collections OrderedMap
Java Program to Implement Suffix Tree
The DAO with Spring and Hibernate
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Jackson Ignore Properties on Marshalling
Overview of the java.util.concurrent
Java Program to Find the GCD and LCM of two Numbers
Spring Security Custom AuthenticationFailureHandler
Java Program to Implement Word Wrap Problem
Using a Mutex Object in Java
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Uniform-Cost Search
String Processing with Apache Commons Lang 3