Caesar Cipher Program in Java with Output

Caesar cipher technique was founded by Julius caesar. Before looking at the caesar cipher program in java with output for encryption and decryption, first, we need to understand the terms plaintext and ciphertext.

Read Also:  Vigenere Cipher Program in Java

What is plaintext and ciphertext?

plaintext is the input message given by user. In other words, message that needs to be encrypted.

ciphertext is the encrypted message. In other words, message after applying the caesar cipher technique.

What is Caesar cipher?

Caesar cipher is one of the simplest encryption technique. It is also known as the shift cipher, Caesar's cipher, Caesar shift or Caesar's code. Caesar cipher is a type of substitution cipher.

By using this cipher technique we can replace each letter in the plaintext with different one a fixed number of places up or down the alphabet.

For example :

With right shift of 3:

plaintext :      ABCDEFGHIJKLMNOPQRSTUVWXYZ

ciphertext :    DEFGHIJKLMNOPQRSTUVWXYZABC


Caesar cipher program in java with output



With left shift of 3:

plaintext :      ABCDEFGHIJKLMNOPQRSTUVWXYZ

ciphertext :    XYZABCDEFGHIJKLMNOPQRSTUVW


Caesar cipher left shift of 3

With right shift of 2:

plaintext :   Java Hungry Blog
ciphertext : Lcxc Jwpita Dnqi

As you can see for the above example "Java Hungry Blog" each character in plain text is shifted by 2 as J become L , a become c , v become x and so on.

Note : You can use either left shift or right shift but not both in same text.

If we want to see Caesar cipher in mathematical way, then formula to get encrypted letter will be :

e = (x + n) mod 26

where,
n is the number of positions we need to shift plaintext characters
x is the place value of original letter
e is the place value of encrypted letter

On the other hand, we will use the below formula to decrypt each letter.

e = (x - n) mod 26

Pseudo Code for Caesar Cipher
  1. Input : String plaintext
  2. Input : An integer between 0 and 25 representing the right shift of the character  or, 
  3. an integer between -25 and -1 representing the left shift of the characters. 
  4. Traverse each character in the plaintext one at a time
  5. Transform the given character depending on encryption or decryption.
  6. print the ciphertext.

Simple Caesar Cipher Program in Java for Encryption 

import java.util.*;
public class CaesarCipherProgram {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println(" Input the plaintext message : ");
        String plaintext = sc.nextLine();
        System.out.println(" Enter the value by which 
        each character in the plaintext         
        message gets shifted : ");
        int shift = sc.nextInt();
        String ciphertext = "";
        char alphabet;
        for(int i=0; i < plaintext.length();i++) 
        {
             // Shift one character at a time
            alphabet = plaintext.charAt(i);
            
            // if alphabet lies between a and z 
            if(alphabet >= 'a' && alphabet <= 'z') 
            {
             // shift alphabet
             alphabet = (char) (alphabet + shift);
             // if shift alphabet greater than 'z'
             if(alphabet > 'z') {
                // reshift to starting position 
                alphabet = (char) (alphabet+'a'-'z'-1);
             }
             ciphertext = ciphertext + alphabet;
            }
            
            // if alphabet lies between 'A'and 'Z'
            else if(alphabet >= 'A' && alphabet <= 'Z') {
             // shift alphabet
             alphabet = (char) (alphabet + shift);    
                
             // if shift alphabet greater than 'Z'
             if(alphabet > 'Z') {
                 //reshift to starting position 
                 alphabet = (char) (alphabet+'A'-'Z'-1);
             }
             ciphertext = ciphertext + alphabet;
            }
            else {
             ciphertext = ciphertext + alphabet;   
            }
        
    }
    System.out.println(" ciphertext : " + ciphertext);
  }
}

Output :

Input the plaintext message :  Java Hungry Blog
Enter the value by which each character in the plaintext message gets shifted :  2
ciphertext : Lcxc Jwpita Dnqi


Simple Caesar Cipher Program in Java for Decryption 

import java.util.*;
public class CaesarCipherProgram {
        public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println(" Input the ciphertext message : ");
        String ciphertext = sc.nextLine();
        System.out.println(" Enter the shift value : ");
        int shift = sc.nextInt();
        String decryptMessage = "";
        for(int i=0; i < ciphertext.length();i++)  

        {
            // Shift one character at a time
            char alphabet = ciphertext.charAt(i);
            // if alphabet lies between a and z 
            if(alphabet >= 'a' && alphabet <= 'z')
            {
                // shift alphabet
                alphabet = (char) (alphabet - shift);
            
                // shift alphabet lesser than 'a'
                if(alphabet < 'a') {
                    //reshift to starting position 
                    alphabet = (char) (alphabet-'a'+'z'+1);
                }
                decryptMessage = decryptMessage + alphabet;
            }    
                // if alphabet lies between A and Z
            else if(alphabet >= 'A' && alphabet <= 'Z')
            {
             // shift alphabet
                alphabet = (char) (alphabet - shift);
                
                //shift alphabet lesser than 'A'
                if (alphabet < 'A') {
                    // reshift to starting position 
                    alphabet = (char) (alphabet-'A'+'Z'+1);
                }
                decryptMessage = decryptMessage + alphabet;            
            }
            else 
            {
             decryptMessage = decryptMessage + alphabet;            
            } 
        }
        System.out.println(" decrypt message : " + decryptMessage);
    }
}

Output :

Input the ciphertext message : Lcxc Jwpita Dnqi
Enter the shift value : 2
decrypt message : Java Hungry Blog

That's all for the post caesar cipher program in java with output. If you have any questions then please mention in the comments below.
 
Image Credits : Cepheus [Public domain], from Wikimedia Commons
Matt_Crypto [Public domain], via Wikimedia Commons

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry