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 Program to Implement Maximum Length Chain of Pairs
Remove the First Element from a List
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Hướng dẫn Java Design Pattern – Interpreter
How to Convert List to Map in Java
String Joiner trong Java 8
How to Break from Java Stream forEach
Giới thiệu Json Web Token (JWT)
Handling Errors in Spring WebFlux
Java Program to Implement Quick Sort Using Randomization
Java Program to Find Nearest Neighbor Using Linear Search
Java Program to Implement Skip List
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Spring RequestMapping
Java 8 Streams peek() API
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Netflix Archaius with Various Database Configurations
Java Program to Implement D-ary-Heap
Read an Outlook MSG file
Guava – Join and Split Collections
Spring Security and OpenID Connect
Java Program to Implement Stein GCD Algorithm
Cachable Static Assets with Spring MVC
Using Spring ResponseEntity to Manipulate the HTTP Response
Converting String to Stream of chars
Java Program to Implement Segment Tree
Hashing a Password in Java
Spring Cloud Series – The Gateway Pattern
Hướng dẫn Java Design Pattern – Transfer Object
Giới thiệu JDBC Connection Pool
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Iterating over Enum Values in Java