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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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:
1 2 3 4 5 6 7 | $ javac CaesarCipher.java $ java CaesarCipher Enter the String for Encryption: Sanfoundry vdqirxqgub sanfoundry |
Related posts:
Java Program to Represent Graph Using Adjacency Matrix
Add Multiple Items to an Java ArrayList
Convert XML to JSON Using Jackson
Map Interface trong java
Java Program to Find Transpose of a Graph Matrix
Spring Boot - Interceptor
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
A Guide To UDP In Java
RegEx for matching Date Pattern in Java
OAuth2.0 and Dynamic Client Registration
Spring WebFlux Filters
Removing all Nulls from a List in Java
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Introduction to Project Reactor Bus
Java Program to Implement Sieve Of Atkin
Java program to Implement Tree Set
Java Program to Implement Treap
Java Program to Generate Random Hexadecimal Byte
Java Program to Print only Odd Numbered Levels of a Tree
JPA/Hibernate Persistence Context
Spring Cloud – Bootstrapping
Introduction to Spring Cloud Netflix – Eureka
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Giới thiệu Design Patterns
Java Program to Find Basis and Dimension of a Matrix
Sắp xếp trong Java 8
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement Pollard Rho Algorithm
Spring Data Reactive Repositories with MongoDB
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Template Engines for Spring
Từ khóa throw và throws trong Java