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:
Chuyển đổi Array sang ArrayList và ngược lại
Receive email by java client
Java – Try with Resources
Java Program to Implement the Hill Cypher
The Guide to RestTemplate
Why String is Immutable in Java?
Một số nguyên tắc, định luật trong lập trình
Spring Boot - Exception Handling
Mảng (Array) trong Java
Java Program to Implement Jarvis Algorithm
Java Program to Implement LinkedList API
Tính đa hình (Polymorphism) trong Java
Registration – Activate a New Account by Email
Hướng dẫn Java Design Pattern – Prototype
Extract links from an HTML page
Java – Write to File
ClassNotFoundException vs NoClassDefFoundError
A Guide to Java SynchronousQueue
Java Program to Solve any Linear Equations
Toán tử instanceof trong java
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Create a Custom Exception in Java
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to Implement Word Wrap Problem
Transaction Propagation and Isolation in Spring @Transactional
Quick Guide to the Java StringTokenizer
Introduction to Spring Security Expressions
Guide to the ConcurrentSkipListMap
Introduction to Spring MVC HandlerInterceptor
Anonymous Classes in Java
Spring Cloud – Bootstrapping
Java Program to Find a Good Feedback Vertex Set