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:
Lớp TreeMap trong Java
Composition, Aggregation, and Association in Java
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Filtering a Stream of Optionals in Java
Spring Boot - Google OAuth2 Sign-In
Mệnh đề Switch-case trong java
Spring Boot - Enabling Swagger2
Luồng Daemon (Daemon Thread) trong Java
Java Program to Implement Gabow Algorithm
Handle EML file with JavaMail
Java 8 Stream API Analogies in Kotlin
Java Program to Implement Brent Cycle Algorithm
Query Entities by Dates and Times with Spring Data JPA
Java Program to Find the Connected Components of an UnDirected Graph
File Upload with Spring MVC
Overview of the java.util.concurrent
Java Program to Generate a Sequence of N Characters for a Given Specific Case
The Difference Between Collection.stream().forEach() and Collection.forEach()
Spring Data MongoDB Transactions
Convert char to String in Java
Introduction to Spring Cloud Stream
Spring Security Remember Me
Java Copy Constructor
Java Program to Implement Word Wrap Problem
Java Program to Implement Quick sort
Java Program to Perform integer Partition for a Specific Case
Transaction Propagation and Isolation in Spring @Transactional
Build a REST API with Spring and Java Config
Java Program to Perform Uniform Binary Search
Hướng dẫn sử dụng Java Reflection