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 Check Whether Graph is DAG
Convert char to String in Java
Tránh lỗi NullPointerException trong Java như thế nào?
Spring Boot - Hystrix
How to Iterate Over a Stream With Indices
Using the Map.Entry Java Class
Registration with Spring Security – Password Encoding
Converting String to Stream of chars
Spring 5 and Servlet 4 – The PushBuilder
A Guide to Apache Commons Collections CollectionUtils
Java Program to Implement AA Tree
Java Program to Implement the One Time Pad Algorithm
Send an email with an attachment
Java Program to Compare Binary and Sequential Search
Adding a Newline Character to a String in Java
Java – Random Long, Float, Integer and Double
Java Program to Implement Suffix Array
Sử dụng CyclicBarrier trong Java
The Registration Process With Spring Security
Java – Convert File to InputStream
Spring Autowiring of Generic Types
Simultaneous Spring WebClient Calls
New Features in Java 13
New Features in Java 10
Guava – Join and Split Collections
Guide to CountDownLatch in Java
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program to Generate Date Between Given Range
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Spring @RequestParam Annotation
Spring – Injecting Collections
Java Perform to a 2D FFT Inplace Given a Complex 2D Array