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:
An Intro to Spring Cloud Vault
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Enum trong java
Feign – Tạo ứng dụng Java RESTful Client
What is a POJO Class?
New Features in Java 13
Using Spring ResponseEntity to Manipulate the HTTP Response
A Guide to System.exit()
Java Program to Implement Heap Sort Using Library Functions
Quick Intro to Spring Cloud Configuration
How to Read a File in Java
Serve Static Resources with Spring
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Converting a Stack Trace to a String in Java
Spring REST API + OAuth2 + Angular
Hướng dẫn Java Design Pattern – Builder
Quick Guide to Spring Controllers
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Build a REST API with Spring and Java Config
Introduction to Eclipse Collections
Apache Commons Collections BidiMap
Iterating over Enum Values in Java
HashSet trong java
Immutable Objects in Java
A Guide to the finalize Method in Java
Java Program to Perform Search in a BST
Lớp Collectors trong Java 8
Spring Security Custom AuthenticationFailureHandler
Java Program to Implement LinkedHashMap API
Java 8 and Infinite Streams
Integer Constant Pool trong Java
Apache Camel with Spring Boot