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:
Lập trình đa luồng trong Java (Java Multi-threading)
Error Handling for REST with Spring
Java Program to Implement Bucket Sort
Batch Processing with Spring Cloud Data Flow
So sánh HashMap và Hashtable trong Java
Assert an Exception is Thrown in JUnit 4 and 5
Serverless Functions with Spring Cloud Function
Spring Boot - Exception Handling
Java Program to Perform Polygon Containment Test
Java Program to Implement Floyd Cycle Algorithm
Java Program to Implement ConcurrentHashMap API
Java Program to Implement PrinterStateReasons API
New Features in Java 13
Java Program to Implement Hash Trie
Guide to Mustache with Spring Boot
Getting Started with Stream Processing with Spring Cloud Data Flow
Java 8 Streams peek() API
Java Program to Implement Repeated Squaring Algorithm
Spring Data JPA and Null Parameters
Java Program to Implement Find all Back Edges in a Graph
Checked and Unchecked Exceptions in Java
Java Program to Perform Naive String Matching
Java 8 Collectors toMap
Most commonly used String methods in Java
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
An Intro to Spring Cloud Contract
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Generate Date Between Given Range
Java Program to Implement SimpeBindings API
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Spring Boot - Tracing Micro Service Logs