Caesar Cipher in Java


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.

  1. 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.
  2. 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.
  3. 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.
  4. 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


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.)


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.

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.


Enter the text message to be encrypted hiarpit
Encrypted Text : lmevtmx
Decryptd Text : hiarpit

That’s all about Caesar Cipher in Java.



  1. Abdulkader Elayah April 9, 2020
    • Arpit Mandliya April 9, 2020

Add Comment