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:
Java Program to Find Strongly Connected Components in Graphs
Java – InputStream to Reader
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java 8 Stream API Analogies in Kotlin
New Features in Java 10
Jackson vs Gson
File Upload with Spring MVC
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Implement Pairing Heap
Registration with Spring Security – Password Encoding
JUnit5 @RunWith
Java Program to Implement Triply Linked List
Java Program to Perform Polygon Containment Test
Java Program to Implement the RSA Algorithm
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Spring Security and OpenID Connect
Java Program to Implement Threaded Binary Tree
Hướng dẫn Java Design Pattern – Flyweight
Từ khóa throw và throws trong Java
Reading an HTTP Response Body as a String in Java
Test a REST API with Java
Java Program to Implement Leftist Heap
Spring Boot Actuator
Java Program to Describe the Representation of Graph using Incidence List
Spring Webflux and CORS
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Implement IdentityHashMap API
Java Program to Find Transitive Closure of a Graph
Java Program to Implement Hash Tree
Exploring the Spring Boot TestRestTemplate
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
A Guide to JPA with Spring