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 Stack
Java Program to Implement Stein GCD Algorithm
Java Program to Implement the MD5 Algorithm
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Implement SynchronosQueue API
Java Program to Implement HashSet API
Java Program to Perform Complex Number Multiplication
Java Program to Implement Binary Heap
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Generate Random Numbers Using Probability Distribution Function
Java Program to Implement Graph Coloring Algorithm
Introduction to Thread Pools in Java
Java String to InputStream
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java 8 – Powerful Comparison with Lambdas
Toán tử trong java
Exploring the Spring Boot TestRestTemplate
Java Program to Generate a Random Subset by Coin Flipping
Java Program to Create the Prufer Code for a Tree
Lập trình hướng đối tượng (OOPs) trong java
Lớp Collectors trong Java 8
Recommended Package Structure of a Spring Boot Project
Validations for Enum Types
Java – Rename or Move a File
Reversing a Linked List in Java
New Stream Collectors in Java 9
Java Program to Implement Treap
Concatenating Strings In Java
Java Program to Perform Deletion in a BST
Lớp Arrarys trong Java (Arrays Utility Class)
Java InputStream to String