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:
ExecutorService – Waiting for Threads to Finish
Java Program to Implement EnumMap API
Java Program to Implement Binary Search Tree
Java Program to Implement LinkedBlockingDeque API
Weak References in Java
Ways to Iterate Over a List in Java
Request Method Not Supported (405) in Spring
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Chương trình Java đầu tiên
Java – Reader to InputStream
Show Hibernate/JPA SQL Statements from Spring Boot
Introduction to Spring Cloud Rest Client with Netflix Ribbon
HttpClient 4 – Follow Redirects for POST
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Implement Gabow Algorithm
Tạo số và chuỗi ngẫu nhiên trong Java
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
A Comparison Between Spring and Spring Boot
HttpAsyncClient Tutorial
Spring Webflux and CORS
Introduction to the Functional Web Framework in Spring 5
Spring JDBC
Simple Single Sign-On with Spring Security OAuth2
Apache Commons Collections SetUtils
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Concatenating Strings In Java
Working with Tree Model Nodes in Jackson
Practical Java Examples of the Big O Notation
Hướng dẫn sử dụng String Format trong Java
New Features in Java 13
Tiêu chuẩn coding trong Java (Coding Standards)
Lớp Arrarys trong Java (Arrays Utility Class)