The Caesar cipher is a technique in which an encryption algorithm is used to change some text for gaining integrity, confidentiality, or security of a message. In cryptography there are many algorithms that are used to achieve the same, but Caesar cipher is the earliest and easiest algorithm used among encryption techniques. This algorithm was named after Julius Caesar who was a Roman general and statesman.
Encryption : The process of changing a given text or message to an encoded format using any encryption algorithm so that it cannot be read normally and can only be accessed by an authorized user.
Decryption : The process of converting the encoded or encrypted message back to its original form. In some algorithms applying the same method can decrypt the encoded message to its original form.
Plaintext : The original message which needs to be sent to the end user. It may or may not be formatted.
Ciphertext : The resulting message formed when an encryption algorithm is applied on the plaintext.
Shift : Integer between zero and twenty-five which can tell us how many shifts will be applied on a character.
In encryption a given message will be transformed into another formatted message. To use the Caesar cipher technique, a shift will be given to us, which will be applied to encrypt our message.
Let’s learn about this with the help of the above example. Suppose we are given a shift of three, then each character of a message will be shifted to the next third character. The last characters like Y or Z will follow the loop and be shifted to A , B or C. With a shift of three, A is shifted to D and B is shifted to E.**
**
Formula for Encryption With Shift n
This means that any letter x is equal to (x + n), where n is the shift number and x is a character. The result will be taken under modulo division if there is a case where any character reaches the end of the alphabet. So the module will take the character to the start of the alphabet.
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
import java.io.*;
import java.util.*;
public class Solution { //to keep track of index
public static final String alpha = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String message, int shiftKey) {
message = message.toLowerCase();
String cipherText = "";
for (int ii = 0; ii < message.length(); ii++) {
int charPosition = alpha.indexOf(message.charAt(ii));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = alpha.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String message = new String();
int key = 0;
System.out.print("Enter the String for Encryption:");
message = sc.next();
System.out.println("\n\nEnter Shift Key:");
key = sc.nextInt();
System.out.println("\nEncrpyted msg:" + encrypt(message, key));
} //main method ends
} //Solution Class End
Code Snippet
Now we will process the cipher message which is encrypted by Caesar cipher to break it down to its original message form. There will be a shiftKey given, using which we can shift each character back to get the original message. Suppose the encrypted text is CDEF and two is the shiftKey, then:
C -> A
D -> B
E -> C
F -> D
That means our original message is “ABCD”.
The Formula for Decryption With Shift n
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
import java.io.*;
import java.util.*;
public class Solution { //to keep track of index
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String decrypt(String cipherText, int shiftKey) {
cipherText = cipherText.toLowerCase();
String message = "";
for (int ii = 0; ii < cipherText.length(); ii++) {
int charPosition = ALPHABET.indexOf(cipherText.charAt(ii));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0) {
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
message += replaceVal;
}
return message;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String message = new String();
int key = 0;
System.out.print("Enter the String for Encryption:");
message = sc.next();
System.out.println("\n\nEnter Shift Key:");
key = sc.nextInt();
// System.out.println("\nEncrpyted msg:"+encrypt(message, key));
System.out.println("\nDecrypted Message:" + decrypt(message, key));
}
}
Code Snippet