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:
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
New Features in Java 8
Một số nguyên tắc, định luật trong lập trình
Java Program to Implement the Program Used in grep/egrep/fgrep
Guide To CompletableFuture
Java Program to Implement Skew Heap
Spring Security – security none, filters none, access permitAll
Base64 encoding và decoding trong Java 8
Converting Strings to Enums in Java
How to Remove the Last Character of a String?
Java 8 Predicate Chain
Disable Spring Data Auto Configuration
Java Program to Implement Sieve Of Sundaram
Java Program to Implement TreeMap API
HttpClient Basic Authentication
Java Program to Implement LinkedHashMap API
Java Program to Check whether Undirected Graph is Connected using DFS
Using JWT with Spring Security OAuth
Spring Boot Application as a Service
Java Program to Implement Solovay Strassen Primality Test Algorithm
Biến trong java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Generate Spring Boot REST Client with Swagger
Java Program to Compare Binary and Sequential Search
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Solve Tower of Hanoi Problem using Stacks
Hướng dẫn Java Design Pattern – Strategy
File Upload with Spring MVC
Interface trong Java 8 – Default method và Static method
Registration – Activate a New Account by Email
Java Program to Implement Triply Linked List
Spring Boot - Tracing Micro Service Logs