Table of Contents
In this post, we will see about Caesar Cipher in Java.
In cryptography, we used to study different algorithms or techniques to encrypt and decrypt a different sets of messages to gain confidentiality, integrity or say some kind of security.
Usually, these things are achieved by implementing such kind of techniques, sometimes clubbed with other algorithms to increase the security level.
In this article, we will discuss about one such encryption technique which is one of the earliest and easiest methods, named after Julius Caesar.
Before we discuss the actual algorithm and procedure of this technique we have to get familiar with the few terms which we might need to understand as a prerequisite to get to know the working of the technique.
- Plaintext : It is text that is not formatted specially, or computed. In simple words, it is nothing but the original message we are planning to send to the end-user.
- Encryption : It is the process by which the given message or say plaintext is encoded using the different encryption algorithms to ensure that only authorized users or parties can decode it and access them for their use.
- Ciphertext : It is the formatted text or says it is the scrambled form of the data after being encrypted.The encoded text or message we get after applying some set of encryption algorithms on the given plaintext, is termed as the ciphertext.
- Decryption : It is the process by which we obtain the encrypted message or the ciphertext back to its original form. In most of the cases, the algorithms are reversible in nature, therefore in those cases, we directly apply the same encryption algorithm in a reverse way.
Note : The terms are explained in the order in which they are used in cryptography.
Now since we know all the related terms let’s discuss the actual algorithm of Caesar Cipher
Algorithm
It is a simple type of substitution cipher, in this, each letter or word of a given text message is replaced by a letter some fixed number down the original alphabet.
We decide that fixed number, for example, if we select that number as 2 then A will be replaced by C, B will be replaced by D, and so on.
This fixed number here indicates the shift, which means the number of positions by which each letter of the text has to be moved down.
Modified Approach
(We can also modify the algorithm for moving up the character it’s up to the user, in that scenario if shift equals 2, A will be replaced by Y, B will be replaced by Z, and so on.)
Implementation
Step 1(Mapping Plaintext) :
We start this by representing or transforming each letter into numbers, therefore the alphabets will be mapped to the numbers starting from 0, example, A = 0, B = 1, . . . . . , Z = 25.
Step 2(Encrypting and Obtaining CipherText) :
To encrypt the plaintext or the message we add the shift, taken as input from the user to the mapped representation of the extracted letter in the Step 1.
We iterate this through the input text and repeat the Step 2 for each alphabet in the text to obtain the ciphertext.
En = ( x + n ) mod 26 , where n represents shift.
Step 3(Decrypting and Obtaining our original Text) :
Now, to decrypt it we follow the same algorithm but in reverse as we discussed before, here we will subtract the shift from the obtained representation in the Step 2 to get back our original text.
Dn = ( x – n ) mod 26 , where n represents shift.
Here is java program to implement Caesar Cipher in java.
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 52 53 54 55 56 57 |
package org.arpit.java2blog; import java.util.Scanner; public class CaesarCipherMain { public static final String alph = "abcdefghijklmnopqrstuvwxyz"; // static because all other functions are static, and we can't reference from a // non-static public static String encoding(String plainT, int shift) { plainT = plainT.toLowerCase(); // converting the text to lowercase String cipherT = ""; // initializing empty string to add alphabets iteratively for (int i = 0; i < plainT.length(); i++) { int mappingV = alph.indexOf(plainT.charAt(i)); // value of each alphabet in integers like for A=0, B=1 ... int enVal = (shift + mappingV) % 26; char Val = alph.charAt(enVal); // the character to be replaced cipherT = cipherT + Val; // adding to ciphertext } return cipherT; } // following same algorithm but in reverse way, plaintext becomes // ciphertext and vice versa public static String decoding(String cipherT, int shift) { cipherT = cipherT.toLowerCase(); // converting the text to lowercase String plainT = ""; // initializing empty string to add alphabets iteratively for (int i = 0; i < cipherT.length(); i++) { int mappingV = alph.indexOf(cipherT.charAt(i)); int deVal = (mappingV - shift) % 26; if (deVal < 0) // to handle the negative values { deVal = alph.length() + deVal; } char Val = alph.charAt(deVal); // the character to be replaced plainT = plainT + Val; // adding to plaintext } return plainT; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter the text message to be encrypted "); String msg = new String(); msg = scan.next(); System.out.println(" Encrypted Text : " + encoding(msg, 4)); System.out.print(" Decryptd Text : "); System.out.print(decoding(encoding(msg, 4), 4)); scan.close(); } } |
The output for the above code is shown below, as we can see the entered text was obtained in encrypted form and then we again decrypted it to obtain the original text.
Output:
Encrypted Text : lmevtmx
Decryptd Text : hiarpit
That’s all about Caesar Cipher in Java.
It shows me error in cipher+Val and plaint+Val
I mean from where it will get them
Hi Abdul,
Apologies for the typos and thanks for pointing out.
I have fixed the code now and let me know if you are facing this issue.