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:
Encode a String to UTF-8 in Java
How to Convert List to Map in Java
Login For a Spring Web App – Error Handling and Localization
A Quick Guide to Spring Cloud Consul
Spring WebClient vs. RestTemplate
Tránh lỗi NullPointerException trong Java như thế nào?
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Implement Vector API
Java Program to Implement Red Black Tree
Tìm hiểu về Web Service
Hướng dẫn Java Design Pattern – Observer
Hướng dẫn sử dụng Java Generics
The DAO with JPA and Spring
Java Program to implement Priority Queue
Composition, Aggregation, and Association in Java
Spring 5 Testing with @EnabledIf Annotation
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Java Program to Perform Arithmetic Operations on Numbers of Size
Spring Cloud – Securing Services
More Jackson Annotations
Java Program to Implement Find all Back Edges in a Graph
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Implement Miller Rabin Primality Test Algorithm
ETags for REST with Spring
Hướng dẫn Java Design Pattern – Template Method
Quick Guide to Spring Controllers
Logging a Reactive Sequence
Spring Boot - Scheduling
Inject Parameters into JUnit Jupiter Unit Tests
Transactions with Spring and JPA
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Check If a String Is Numeric in Java