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 Check the Connectivity of Graph Using BFS
Java Program to Implement Flood Fill Algorithm
Java Program to Implement Selection Sort
A Guide to Java 9 Modularity
LinkedHashSet trong Java hoạt động như thế nào?
Create Java Applet to Simulate Any Sorting Technique
Using Spring @ResponseStatus to Set HTTP Status Code
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
JWT – Token-based Authentication trong Jersey 2.x
Compare Two JSON Objects with Jackson
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Implement Naor-Reingold Pseudo Random Function
Spring Boot - Tomcat Deployment
Java Concurrency Interview Questions and Answers
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Implement Patricia Trie
Java Program to Find Nearest Neighbor for Dynamic Data Set
Introduction to Spring Boot CLI
Java Program to Implement Counting Sort
Zipping Collections in Java
Java Program to Find the GCD and LCM of two Numbers
Jackson – JsonMappingException (No serializer found for class)
Java Program to Implement Binomial Heap
Using Optional with Jackson
Auditing with JPA, Hibernate, and Spring Data JPA
An Example of Load Balancing with Zuul and Eureka
Java Program to Represent Graph Using Linked List
Java Program to Implement Self organizing List
The Basics of Java Security
Template Engines for Spring
Java Program to Find a Good Feedback Vertex Set
Hướng dẫn Java Design Pattern – Iterator