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:
Quick Guide to Spring MVC with Velocity
Adding Parameters to HttpClient Requests
Java Program to Find Basis and Dimension of a Matrix
Java 8 Collectors toMap
Java 8 – Powerful Comparison with Lambdas
Java Program to Describe the Representation of Graph using Incidence Matrix
HashSet trong Java hoạt động như thế nào?
REST Web service: Basic Authentication trong Jersey 2.x
Java Program to Implement Hash Tables with Linear Probing
Introduction to the Java ArrayDeque
Java Program to Implement Range Tree
Chuyển đổi từ HashMap sang ArrayList
Guide to the Java TransferQueue
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Spring Boot - Flyway Database
Custom Thread Pools In Java 8 Parallel Streams
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Implement Attribute API
Java Program to Implement LinkedBlockingDeque API
Giới thiệu SOAP UI và thực hiện test Web Service
Hướng dẫn sử dụng Java Annotation
Spring Boot - Zuul Proxy Server and Routing
Jackson Exceptions – Problems and Solutions
Testing in Spring Boot
Phương thức tham chiếu trong Java 8 – Method References
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Transactions with Spring and JPA
Spring Cloud AWS – RDS
Spring MVC Custom Validation
Java Program to Perform LU Decomposition of any Matrix
Jackson vs Gson
Java Program to Implement Strassen Algorithm