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:
Unsatisfied Dependency in Spring
Java Program to Generate All Possible Combinations of a Given List of Numbers
Annotation trong Java 8
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Implement Euclid GCD Algorithm
Java Program to Permute All Letters of an Input String
Java – Reader to String
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Map With Case-Insensitive Keys
Spring Webflux and CORS
Cơ chế Upcasting và Downcasting trong java
A Guide to the finalize Method in Java
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Explain about URL and HTTPS protocol
Java Program to Implement LinkedHashMap API
Reading an HTTP Response Body as a String in Java
Guide to Mustache with Spring Boot
An Example of Load Balancing with Zuul and Eureka
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Implement Solovay Strassen Primality Test Algorithm
Guide to the Fork/Join Framework in Java
REST Pagination in Spring
Logging a Reactive Sequence
Java Program to Check if it is a Sparse Matrix
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement Gale Shapley Algorithm
Object cloning trong java
Java Program to Solve the Fractional Knapsack Problem
Changing Annotation Parameters At Runtime
Introduction to Spring Cloud OpenFeign
How to use the Spring FactoryBean?