Caesar Cipher Program in Java

In the article, we are going to study about caesar program in java where first we will be starting things with what exactly caesar cipher is and upon progressing with theory and concept related to the the topic, we will study dry run, logic, algorithm, code for caesar cipher program in java. Moreover, the analysis of code will be done to get an in-depth knowledge and concept relative to the topic.

What is Caesar Cipher

As off-topic it might sound first, Caesar Cipher is an interesting problem that dates back to the time of Julius Caesar, when information was passed by altering the original message in the similar manner to what modern day encryption-decryption phenomena is practiced.

Encryption is performed by on obtaining the shift input, n and shifting each character in string by n places such that if character B needs to to be shifted by 3 places, then B->C->D->E can define the transition well.

The reverse translation to its original state i.e. B can be termed as Decryption of character. This process of substitution is applied on each character of the word in order to call it as Caesar Cipher.

The following two formulae tells us that we can sum up the shift value and modulo to find value that exceed the unit value of highest alphabet, z i.e. 26.

Dry Run of Caesar Cipher Program in Java

Now that we have a brighter picture on the question of caesar cipher logic actually works. Let us trace an example in a step by step manner to see how it works.

We have our input word, ATLAS and our job is to first encrypt and decrypt it again to its original form, we can do it in as per the step by step process illustrated and explained below.

In the above illustration, A was moved by three places and placed as D in the result string.

The alteration made of T with change of +3 in 1st index will lead to storage of V in the string.

At index 2, L was replaced with O, that is stored in the 2nd index of result string.

Similar to the operation at the 0th index, here also, A is going to turn into D when shifted by 3 positions. Thus D is going to be stored in the resultant string.

The final operation on the last index to replace S in a similar fashion, leads to V getting stored in the result string.

Algorithm of Caesar Cipher Program in Java

In the previous section, we discussed how to substitute character values using the shifting integer on all the characters stored in string. Now let us have a look at what can be a clean algorithm to solidify the knowledge of the Caesar cipher program in java.


  1. Take a string and a shifting integer lying between the range of 1-25
  2. Create new result string
  3. Iterate for each character in string:
    • Shift the character by n places depending if it is being encrypted or decrypted and store it as char type, newchar.
    • Add the shifted character newchar, in the result string.
  4. Return or print the string

Code for Caesar Cipher Program In Java

Having a clear understanding of how each step unfolds by going through the dry run and algorithm portion in this caesar cipher program in Java, let us move to the code part finally.

class CaesarCipher {

    public static StringBuffer encrypt(String plaintext, int shift) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < plaintext.length(); i++) {
            if (Character.isUpperCase(plaintext.charAt(i))) {
                char ch = (char)(((int)plaintext.charAt(i) +
                                  shift - 65) % 26 + 65);
            } else {
                char ch = (char)(((int)plaintext.charAt(i) +
                                  shift - 97) % 26 + 97);
        return result;
    public static void main(String[] args) {
        String plaintext = "ATLAS";
        int shift = 3;
        System.out.println("Plaintext  : " + plaintext + "\n");
        System.out.println("Shift : " + shift + "\n");
        System.out.println("Ciphertext: " + encrypt(plaintext, shift));

Explanation: In the driver code, plain text string and shift integer are taken from the user and passed to the encrypt function where the result is declared using StringBuffer() to hold the result.

The condition is applied on each character of the plaintext with shifting performed inside the loop in the program. In the end, we return the result back to the main function to print the result.


Plaintext  : ATLAS

Shift : 3

Ciphertext: DWODV

Analysis of Caesar Cipher Program in Java

Coming to the analysis of code for the Caesar cipher program in java, it is evident from the logic that we will traverse each character in order to alter the value for encryption, thus the code is going to have a linear complexity in the worst case. Hence, the time complexity is denoted by O(N).

As far as the space complexity is concerned, we will be holding string in a stringBuffer that can hold upto N values. Hence the auxiliary space complexity tends to be O(N) as well.

In this article, we studied that Caesar cipher is used for encryption and decryption on texts and named after its use in Julius Caesar era as a token of confidentiality.

In the further section, we understood the algorithm and code for the Caesar Cipher Program in Java. Hope you liked this article on Caesar Cipher Program in Java. We hope to see you with another helpful article in future.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java
Number Pattern Program in Java
For Loop Program In Java
Pattern Program in Java
String Palindrome Program in Java
Thread Program in JAVA
Java Scanner Program
While Loop Program in Java
Bubble Sort Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *