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:
Jackson – Change Name of Field
Working with Kotlin and JPA
Java – String to Reader
An Intro to Spring Cloud Task
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Spring Security 5 for Reactive Applications
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Sử dụng CountDownLatch trong Java
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Implement Shunting Yard Algorithm
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Spring Security Authentication Provider
Spring Boot - Cloud Configuration Client
Java Program to Implement Binary Heap
Java Program to Implement Treap
Changing Annotation Parameters At Runtime
Create a Custom Auto-Configuration with Spring Boot
Java Program to Check Whether Graph is DAG
Java Program to Implement Threaded Binary Tree
Giới thiệu java.io.tmpdir
Spring Boot - Database Handling
Java Program to Check whether Undirected Graph is Connected using BFS
HttpClient Timeout
Working With Maps Using Streams
A Guide to TreeMap in Java
Guide to Guava Table
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Cơ chế Upcasting và Downcasting trong java
Validate email address exists or not by Java Code
Java Program to Perform Insertion in a BST
The Spring @Controller and @RestController Annotations
Giới thiệu SOAP UI và thực hiện test Web Service