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 Bit Matrix
File Upload with Spring MVC
Java Program to Implement SynchronosQueue API
Java Program to Implement Warshall Algorithm
Các nguyên lý thiết kế hướng đối tượng – SOLID
Java Program to Evaluate an Expression using Stacks
Convert char to String in Java
JUnit5 @RunWith
Spring Security Registration – Resend Verification Email
Spring Webflux with Kotlin
Java Program to Find Minimum Element in an Array using Linear Search
Java – Try with Resources
Tránh lỗi NullPointerException trong Java như thế nào?
A Guide to the Java LinkedList
Java Program to Compute Determinant of a Matrix
Getting Started with Forms in Spring MVC
Java Program to Implement DelayQueue API
Introduction to Spring MVC HandlerInterceptor
Java Program to Implement Sorted Vector
Giới thiệu JDBC Connection Pool
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Spring Boot - Enabling HTTPS
ClassNotFoundException vs NoClassDefFoundError
Hướng dẫn Java Design Pattern – Memento
Java Program to Implement Suffix Tree
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement LinkedHashSet API
Documenting a Spring REST API Using OpenAPI 3.0
A Guide to Concurrent Queues in Java
Optional trong Java 8
Intro to Spring Boot Starters
Java Program to Implement Uniform-Cost Search