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:
Guide to Mustache with Spring Boot
Java Program to Find Nearest Neighbor for Dynamic Data Set
Spring Webflux and CORS
Guide to Java OutputStream
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Wiring in Spring: @Autowired, @Resource and @Inject
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Spring Cloud AWS – EC2
Spring Boot - Code Structure
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Implement the Checksum Method for Small String Messages and Detect
Check If a File or Directory Exists in Java
Spring Boot - Rest Template
Hướng dẫn Java Design Pattern – Proxy
Java Program to Implement Doubly Linked List
Spring Boot Actuator
Spring Security Remember Me
Logging a Reactive Sequence
Control the Session with Spring Security
Java Program to Implement Sorted Circular Doubly Linked List
Java Program to Implement Sparse Array
Intersection of Two Lists in Java
Tính đa hình (Polymorphism) trong Java
Circular Dependencies in Spring
Toán tử instanceof trong java
Easy Ways to Write a Java InputStream to an OutputStream
Converting Java Date to OffsetDateTime
Java Program to Implement CopyOnWriteArraySet API
Deploy a Spring Boot App to Azure