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:
Working with Tree Model Nodes in Jackson
Map Interface trong java
Java Program to Implement Shoelace Algorithm
Đồng bộ hóa các luồng trong Java
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Finding the Differences Between Two Lists in Java
Assertions in JUnit 4 and JUnit 5
Primitive Type Streams in Java 8
Java Program to Find Transpose of a Graph Matrix
Java – Delete a File
Custom JUnit 4 Test Runners
How to Convert List to Map in Java
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Hướng dẫn sử dụng Lớp FilePermission trong java
Spring Autowiring of Generic Types
Java Program to Implement AVL Tree
Spring Boot - CORS Support
Supplier trong Java 8
Java Program to Find the Edge Connectivity of a Graph
Spring Boot - Tomcat Deployment
Java Program to Implement Sorted Circularly Singly Linked List
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Spring 5 and Servlet 4 – The PushBuilder
Spring Cloud Series – The Gateway Pattern
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java Program to Implement Max Heap
Java Program to Implement Ternary Search Tree
Hướng dẫn Java Design Pattern – Service Locator
Java 8 Streams peek() API
Upload and Display Excel Files with Spring MVC