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 Kosaraju Algorithm
Java – String to Reader
Java Program to Implement Radix Sort
Spring Security and OpenID Connect
Java Program to Implement Network Flow Problem
Converting Between a List and a Set in Java
Iterating over Enum Values in Java
Java Program to Implement Rope
Java Program to Perform Uniform Binary Search
A Guide to ConcurrentMap
Java Program to Compute the Area of a Triangle Using Determinants
Date Time trong Java 8
Chuyển đổi giữa các kiểu dữ liệu trong Java
Toán tử trong java
Java Program to Construct an Expression Tree for an Infix Expression
Ways to Iterate Over a List in Java
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Spring REST with a Zuul Proxy
Object cloning trong java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to Implement Interval Tree
Adding Parameters to HttpClient Requests
Spring Boot - Interceptor
Dockerizing a Spring Boot Application
HttpClient Connection Management
Debug a JavaMail Program
Sử dụng CountDownLatch trong Java
Java Program to Implement HashMap API
Spring JDBC
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Implement Maximum Length Chain of Pairs