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:
Introduction to Apache Commons Text
Spring Web Annotations
Quick Guide on Loading Initial Data with Spring Boot
Tìm hiểu về Web Service
Java Program to Implement Sorted Circular Doubly Linked List
Java Program to Implement Karatsuba Multiplication Algorithm
Jackson JSON Views
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Implement Graph Structured Stack
New Features in Java 11
Exploring the New Spring Cloud Gateway
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Represent Graph Using Adjacency Matrix
Java Program to Check whether Graph is a Bipartite using BFS
REST Web service: Basic Authentication trong Jersey 2.x
List Interface trong Java
Java Program to Perform Sorting Using B-Tree
Removing Elements from Java Collections
Setting Up Swagger 2 with a Spring REST API
Extract network card address
Guide to @ConfigurationProperties in Spring Boot
Lớp Collections trong Java (Collections Utility Class)
Java Program to Generate a Random Subset by Coin Flipping
Java Program to Implement Gabow Algorithm
Hướng dẫn Java Design Pattern – Chain of Responsibility
Removing all duplicates from a List in Java
Using a Mutex Object in Java
Java Program to Perform integer Partition for a Specific Case
Ép kiểu trong Java (Type casting)
Hướng dẫn Java Design Pattern – Memento
Hướng dẫn Java Design Pattern – Decorator
Upload and Display Excel Files with Spring MVC