Caesar Cipher Program in Java
It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of positions below the alphabet.
With such a shift of 1, for instance, X would become Y, Y would turn into Z, and so on. The first person to use it to communicate with his authorities was Julius Caesar. This method was given the name Caesar Cipher Method in honor of the man.
The ciphering of a given text requires an integer value. Each letter of a text has been shifted down a certain number of positions, as indicated by the integer value known as shift.
The following is how a shift n can be used to express the encrypting of a letter mathematically:
Shift n encryption phase: En (x) = (x+n)mod 26
Phase of decryption with n = Dn (x) = (x-n)mod 26
Examples
ABCDEFGHIJKLMNOPQRSTUVW XYZ : Text
Shift: 23
XYZABCDEFGHIJKLMNOPQRSTUVW is the cipher.
ATTACKATONCE: Text
Shift: 4
Encryption: EXXEGOEXSRGI
The following procedures are used to put the Caesar Cipher software into practice:
- Take a user-supplied input string and encrypt it by employing the Caesar Cipher method.
- For character shifting, ask the user for an input number. The input integer must range from 0 to 25.
- Character by character, iterate through the supplied string.
- We change each character in accordance with the rule based just on encryption and decoding.
- brings back the freshly created string.
Let's put the code again for the Caesar Cipher approach into practice using the procedures that were just covered.
CaesarCipherExpl.java
// import all the required classes and also the packages
import java.util.Scanner;
// creating the class CaesarCipherExpl for decryption and encryption
public class CaesarCipherExpl
{ // alphabet string denotes alphabet from a to z
public static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
// create encryptData() method for encrypting user iS with given sK
public static String encryptData(String iS, int sK)
{ // converting the iS into lower case
iS = iS.toLowerCase();
// eS to store encrypted data
String eS = "";
// use the for loop to traverse each character of the iS
for (int j = 0; j < iS.length(); j++)
{ // getting the position of each character of iS in the alphabet
int p = alphabet.indexOf(iS.charAt(j));
// getting eP for each char of iS
int eP = (sK + p) % 26;
char eC = alphabet.charAt(eP);
// adding eC to eS
eS += eC;
}
// returning the eS
return eS;
}
// create decryptData() method for decrypting user iS with given sK
public static String decryptData(String iS, int sK)
{ // converting the iS into lower case
iS = iS.toLowerCase();
// dS for storing the decrypted data
String dS = "";
// using the for loop for traversing each character of the iS
for (int j = 0; j < iS.length(); j++)
{
// get the position of each character of iS in the alphabet
int p = alphabet.indexOf(iS.charAt(j));
// get dC for each char of iS
int dP = (p - sK) % 26;
// if dP is negative
if (dP < 0){
dP = alphabet.length() + dP;
}
char dC = alphabet.charAt(dP);
// adding the dC to the dS
dS += dC;
}
// returning the dS
return dS;
}
// Driver code
public static void main(String[] args)
{
// create an object for the Scanner class
Scanner s = new Scanner(System.in);
// taking input from the user
System.out.println(" Enter the following string to encrypt using the Caesar Cipher: ");
String iS = s.nextLine();
System.out.println(" The amount by which each character as in plaintext message is shifted should be entered is: ");
int sK = Integer.valueOf(s.nextLine());
System.out.println(" Encrypted Data === "+encryptData(iS, sK));
System.out.println(" Decrypted Data === "+decryptData(encryptData(iS, sK), sK));
// closing the Scanner class object
s.close();
}
}
Output:
