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:
Exploring the Spring Boot TestRestTemplate
Guide to Java 8’s Collectors
Java Multi-line String
Java Program to Implement Sieve Of Eratosthenes
Remove the First Element from a List
Java Program to Check for balanced parenthesis by using Stacks
Form Validation with AngularJS and Spring MVC
Sử dụng CountDownLatch trong Java
Cài đặt và sử dụng Swagger UI
Java Program to Show the Duality Transformation of Line and Point
Guide to @JsonFormat in Jackson
Spring Webflux with Kotlin
Java Program to Generate a Random Subset by Coin Flipping
Java Program to Check if it is a Sparse Matrix
Java Program to Implement LinkedBlockingQueue API
Introduction to Liquibase Rollback
Java Program to Implement Bloom Filter
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to implement Dynamic Array
Java Program to Check whether Undirected Graph is Connected using BFS
Fixing 401s with CORS Preflights and Spring Security
Request Method Not Supported (405) in Spring
Validations for Enum Types
Java Program to Implement ArrayDeque API
Java Program to Implement Adjacency List
Multi Dimensional ArrayList in Java
Java Program to Perform integer Partition for a Specific Case
Template Engines for Spring
Java Program to Implement Binary Search Tree
REST Pagination in Spring