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:
Feign – Tạo ứng dụng Java RESTful Client
Disable Spring Data Auto Configuration
Java Program to Implement Circular Doubly Linked List
HTTP Authentification and CGI/Servlet
A Guide to the ResourceBundle
Java Program to Implement Borwein Algorithm
A Guide to TreeMap in Java
Reversing a Linked List in Java
Functional Interfaces in Java 8
Immutable Map Implementations in Java
Java Program to Implement the One Time Pad Algorithm
Netflix Archaius with Various Database Configurations
Getting Started with GraphQL and Spring Boot
Java Program to Implement Strassen Algorithm
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Java Program to Check for balanced parenthesis by using Stacks
New Features in Java 10
Spring MVC Content Negotiation
Fixing 401s with CORS Preflights and Spring Security
Đồng bộ hóa các luồng trong Java
Immutable Objects in Java
Giới thiệu Json Web Token (JWT)
Java 8 Stream API Analogies in Kotlin
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Removing Elements from Java Collections
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Phương thức tham chiếu trong Java 8 – Method References
So sánh Array và ArrayList trong Java
Interface trong Java 8 – Default method và Static method
Posting with HttpClient
Control the Session with Spring Security
Truyền giá trị và tham chiếu trong java