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 Create a Balanced Binary Tree of the Incoming Data
Cài đặt và sử dụng Swagger UI
Convert Hex to ASCII in Java
Checked and Unchecked Exceptions in Java
Java Web Services – JAX-WS – SOAP
Send email with JavaMail
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Introduction to Spring Data JDBC
Java Program to Implement Selection Sort
JUnit 5 @Test Annotation
Implementing a Binary Tree in Java
Java Program to Implement Solovay Strassen Primality Test Algorithm
Java String to InputStream
Java Program to Implement Repeated Squaring Algorithm
Prevent Brute Force Authentication Attempts with Spring Security
Spring WebClient Requests with Parameters
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Giới thiệu java.io.tmpdir
Java Program to Implement Randomized Binary Search Tree
Một số nguyên tắc, định luật trong lập trình
An Introduction to ThreadLocal in Java
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Implement Hopcroft Algorithm
Một số từ khóa trong Java
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Converting between an Array and a List in Java
Spring Boot: Customize the Jackson ObjectMapper
Converting Between Byte Arrays and Hexadecimal Strings in Java
Spring MVC Tutorial
Guide to UUID in Java
Tìm hiểu về Web Service