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:
Function trong Java 8
Sorting in Java
Hướng dẫn Java Design Pattern – Facade
Lớp lồng nhau trong java (Java inner class)
Java Program to Find Transpose of a Graph Matrix
How to Round a Number to N Decimal Places in Java
How to Iterate Over a Stream With Indices
Introduction to the Java NIO2 File API
Write/Read cookies using HTTP and Read a file from the internet
Java Program to Implement Insertion Sort
Java Program to Implement Network Flow Problem
Object Type Casting in Java
Java Program to Check whether Directed Graph is Connected using BFS
Spring Data JPA and Null Parameters
An Intro to Spring Cloud Security
Java Program to Solve TSP Using Minimum Spanning Trees
Beans and Dependency Injection
Handling Errors in Spring WebFlux
Java Program to Implement Merge Sort Algorithm on Linked List
List Interface trong Java
Java – Generate Random String
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
StringBuilder vs StringBuffer in Java
Date Time trong Java 8
Logging in Spring Boot
Java Program to Check Cycle in a Graph using Graph traversal
Hướng dẫn sử dụng lớp Console trong java
Java Program to implement Priority Queue
Bootstrap a Web Application with Spring 5
Removing all Nulls from a List in Java
Java Program to Construct K-D Tree for 2 Dimensional Data
JWT – Token-based Authentication trong Jersey 2.x