This is a java program to implement Caesar Cipher Encryption algorithm. This is the simplest of all, where every character of the message is replaced by its next 3rd character.
Here is the source code of the Java Program to Implement Caesar Cypher. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
package com.maixuanviet.setandstring;
import java.util.Scanner;
public class CaesarCipher
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String plainText, int shiftKey)
{
plainText = plainText.toLowerCase();
String cipherText = "";
for (int i = 0; i < plainText.length(); i++)
{
int charPosition = ALPHABET.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public static String decrypt(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase();
String plainText = "";
for (int i = 0; i < cipherText.length(); i++)
{
int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String for Encryption: ");
String message = new String();
message = sc.next();
System.out.println(encrypt(message, 3));
System.out.println(decrypt(encrypt(message, 3), 3));
sc.close();
}
}
Output:
$ javac CaesarCipher.java $ java CaesarCipher Enter the String for Encryption: Sanfoundry vdqirxqgub sanfoundry
Related posts:
Java 8 StringJoiner
Java – Write a Reader to File
Spring Security Registration – Resend Verification Email
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Jackson Date
Spring Boot: Customize Whitelabel Error Page
Apache Commons Collections BidiMap
Spring Boot - Exception Handling
Lớp Arrarys trong Java (Arrays Utility Class)
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Runnable vs. Callable in Java
Spring Boot - Admin Server
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Spring 5 and Servlet 4 – The PushBuilder
So sánh ArrayList và Vector trong Java
Java Program to Delete a Particular Node in a Tree Without Using Recursion
HttpAsyncClient Tutorial
Fixing 401s with CORS Preflights and Spring Security
Java Program to Implement Depth-limited Search
Giới thiệu Google Guice – Injection, Scope
Interface trong Java 8 – Default method và Static method
Redirect to Different Pages after Login with Spring Security
A Guide to Apache Commons Collections CollectionUtils
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Derived Query Methods in Spring Data JPA Repositories
Servlet 3 Async Support with Spring MVC and Spring Security
StringBuilder vs StringBuffer in Java
A Guide to the Java LinkedList
Java Program to Implement Sieve Of Sundaram
Java Program to Implement Selection Sort
A Custom Data Binder in Spring MVC
Jackson – Marshall String to JsonNode