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:
Send email with SMTPS (eg. Google GMail)
An Intro to Spring Cloud Contract
@DynamicUpdate with Spring Data JPA
Guide to Java Instrumentation
Disable DNS caching
A Guide to System.exit()
Spring WebClient Filters
Spring Boot - Google Cloud Platform
Java Program to Find Number of Articulation points in a Graph
Hướng dẫn sử dụng Java Generics
Truyền giá trị và tham chiếu trong java
An Intro to Spring Cloud Task
Java Program to Perform Partition of an Integer in All Possible Ways
Converting Strings to Enums in Java
Spring Security 5 – OAuth2 Login
Spring 5 Functional Bean Registration
Fixing 401s with CORS Preflights and Spring Security
Explain about URL and HTTPS protocol
Setting a Request Timeout for a Spring REST API
Java Program to Implement LinkedHashMap API
Spring MVC Content Negotiation
HTTP Authentification and CGI/Servlet
Java – InputStream to Reader
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Implement Double Order Traversal of a Binary Tree
Java Program to Compare Binary and Sequential Search
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Implement Singly Linked List
Java – Create a File
Spring WebClient and OAuth2 Support
Creating a Web Application with Spring 5
OAuth 2.0 Resource Server With Spring Security 5