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 Implement the linear congruential generator for Pseudo Random Number Generation
Hướng dẫn sử dụng Lớp FilePermission trong java
Java Program to Implement Fermat Primality Test Algorithm
Một số từ khóa trong Java
Converting a Stack Trace to a String in Java
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Map to String Conversion in Java
Biến trong java
Java Program to Perform Quick Sort on Large Number of Elements
Java Program to Implement Disjoint Sets
Java Program to Perform Arithmetic Operations on Numbers of Size
Automatic Property Expansion with Spring Boot
Guide to Guava Table
Spring WebClient and OAuth2 Support
Difference Between Wait and Sleep in Java
Java Program to find the maximum subarray sum O(n^2) time(naive method)
New Stream Collectors in Java 9
An Intro to Spring Cloud Contract
Dockerizing a Spring Boot Application
Spring Cloud AWS – S3
New Features in Java 13
Spring Boot Application as a Service
Consumer trong Java 8
Java Program to Generate N Number of Passwords of Length M Each
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Apache Commons Collections OrderedMap
Java Program to Perform String Matching Using String Library
Spring Boot - Web Socket
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Java Program to Implement Ternary Tree
Spring Boot - Tracing Micro Service Logs
Changing Annotation Parameters At Runtime