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 JobStateReasons API
Java 8 Collectors toMap
Implementing a Binary Tree in Java
Spring RestTemplate Request/Response Logging
Java Program to Implement Splay Tree
Spring Cloud AWS – EC2
Java Program to implement Priority Queue
Spring Boot Gradle Plugin
Hướng dẫn Java Design Pattern – State
Java Program to Compare Binary and Sequential Search
An Introduction to ThreadLocal in Java
Java Program to Implement Affine Cipher
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Returning Image/Media Data with Spring MVC
So sánh Array và ArrayList trong Java
Java Program to implement Bi Directional Map
Java Program to Implement ConcurrentLinkedQueue API
Date Time trong Java 8
A Guide to HashSet in Java
Documenting a Spring REST API Using OpenAPI 3.0
Compact Strings in Java 9
Java Concurrency Interview Questions and Answers
Java Program to Implement Tarjan Algorithm
Debug a HttpURLConnection problem
A Guide to Java HashMap
Java Program to Implement Find all Forward Edges in a Graph
Spring Security Form Login
Java Collections Interview Questions
Life Cycle of a Thread in Java
Spring Boot Annotations
The Basics of Java Security