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 Expression Tree
Jackson JSON Views
Java Program to Solve Knapsack Problem Using Dynamic Programming
Spring WebClient vs. RestTemplate
RegEx for matching Date Pattern in Java
Lập trình hướng đối tượng (OOPs) trong java
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Using JWT with Spring Security OAuth (legacy stack)
Quick Guide to Spring Bean Scopes
Java Program to Implement Depth-limited Search
Spring Boot - Code Structure
Dockerizing a Spring Boot Application
Tạo chương trình Java đầu tiên sử dụng Eclipse
New Features in Java 15
Mảng (Array) trong Java
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Xử lý ngoại lệ trong Java (Exception Handling)
Spring Boot - Admin Client
Java Program to Implement Queue
Spring NoSuchBeanDefinitionException
@Lookup Annotation in Spring
Using a Mutex Object in Java
Easy Ways to Write a Java InputStream to an OutputStream
Java InputStream to Byte Array and ByteBuffer
Java Program to Implement Max-Flow Min-Cut Theorem
Introduction to Spring Cloud OpenFeign
wait() and notify() Methods in Java
Concatenating Strings In Java
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Program to Implement Stein GCD Algorithm
Spring Data MongoDB – Indexes, Annotations and Converters
Guide to WeakHashMap in Java