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 Radix Sort
Comparing Objects in Java
Tạo số và chuỗi ngẫu nhiên trong Java
Java Program to Implement AttributeList API
Java Program to Perform Arithmetic Operations on Numbers of Size
Send email with SMTPS (eg. Google GMail)
Beans and Dependency Injection
Custom Error Pages with Spring MVC
Introduction to the Java NIO Selector
Java Program to Implement LinkedHashSet API
A Guide to Java HashMap
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java – Rename or Move a File
Comparing Strings in Java
Sending Emails with Java
How to Define a Spring Boot Filter?
Java Program to Represent Graph Using Linked List
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Implement Direct Addressing Tables
So sánh Array và ArrayList trong Java
Java Program to Check the Connectivity of Graph Using DFS
Java Program to Check Whether Graph is DAG
Mảng (Array) trong Java
Java Program to Implement Hash Tables
A Quick Guide to Spring MVC Matrix Variables
Introduction to Project Reactor Bus
A Guide to JUnit 5 Extensions
The Java 8 Stream API Tutorial
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Implement Splay Tree