Table of contents
1.
Introduction
2.
Algorithm for Caesar Cipher
3.
Problem Statement
4.
Problem Solution
5.
Program/Source Code
6.
Advantages of Caesar Cipher
7.
Disadvantages of Caesar Cipher
8.
Frequently Asked Questions
8.1.
What is the purpose of the Caesar Cipher?
8.2.
How do you break a Caesar Cipher?
8.3.
Can the Caesar Cipher be used for real-world security?
9.
Conclusion
Last Updated: Mar 4, 2025
Easy

Caesar Cipher Program in Java

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Caesar Cipher is one of the simplest encryption techniques in cryptography. It works by shifting each letter in a message by a fixed number of positions in the alphabet. This technique is used for basic encryption and decryption of text. 

Caesar Cipher Program in Java

In this article, we will learn how to implement a Caesar Cipher program in Java, including encoding and decoding functions with examples to understand its working.

Algorithm for Caesar Cipher

The Caesar Cipher works on the following logic:

  1. Choose a shift key value (e.g., shift by 3 places).
     
  2. Replace each letter in the plaintext with a letter found after shifting by the chosen key in the alphabet.
     
  3. If the shift moves past 'Z' or 'z', wrap around to the beginning of the alphabet.
     
  4. Decrypting follows the same process but shifts in the opposite direction.

Problem Statement

We need to write a Java program that:

  • Takes a plaintext message as input.
     
  • Accepts a shift key value.
     
  • Encrypts the message using the Caesar Cipher technique.
     
  • Decrypts the message back to its original form.

Problem Solution

To solve this problem, we need to:

  1. Read the input string (plaintext) and shift value.
     
  2. Convert the characters of the string using the shift value.
     
  3. Handle uppercase and lowercase letters separately.
     
  4. Print the encrypted text.
     
  5. Implement a decryption function to retrieve the original text.

Program/Source Code

Here is the Java program to implement the Caesar Cipher:

import java.util.Scanner;

public class CaesarCipher {
    
    public static String encrypt(String text, int shift) {
        StringBuilder result = new StringBuilder();
        
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (Character.isLetter(ch)) {
                char base = Character.isUpperCase(ch) ? 'A' : 'a';
                result.append((char) ((ch - base + shift) % 26 + base));
            } else {
                result.append(ch);
            }
        }
        return result.toString();
    }
    
    public static String decrypt(String text, int shift) {
        return encrypt(text, 26 - shift); // Reverse shift to decrypt
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter text to encrypt: ");
        String text = scanner.nextLine();
        
        System.out.print("Enter shift value: ");
        int shift = scanner.nextInt();
        
        String encryptedText = encrypt(text, shift);
        System.out.println("Encrypted Text: " + encryptedText);
        
        String decryptedText = decrypt(encryptedText, shift);
        System.out.println("Decrypted Text: " + decryptedText);
        
        scanner.close();
    }
}
You can also try this code with Online Java Compiler
Run Code

Program Explanation

  1. encrypt() Method:
    • Iterates through each character of the input string.
       
    • Check if it is a letter.
       
    • Applies the shift and ensures it remains within alphabet bounds.
       
    • Appends the modified character to the result.
       
  2. decrypt() Method:
    • Calls encrypt() with the reverse shift (26 - shift) to restore the original message.
       
  3. main() Method:
    • Takes user input for the message and shift value.
       
    • Calls encrypt() to generate the encrypted text.
       
    • Calls decrypt() to retrieve the original message.
       

Example Output:

Enter text to encrypt: Hello World
Enter shift value: 3
Encrypted Text: Khoor Zruog
Decrypted Text: Hello World

Advantages of Caesar Cipher

  1. Simple and easy to implement – Requires minimal coding effort.
     
  2. Fast encryption and decryption – The shifting operation is efficient.
     
  3. Good for learning purposes – Helps understand the basics of cryptography.

Disadvantages of Caesar Cipher

  1. Weak security – Easily breakable using brute force (only 25 possible shifts).
     
  2. No key variability – A fixed shift does not provide strong encryption.
     
  3. Only works with alphabetic data – Numbers and symbols are not encrypted effectively.

Frequently Asked Questions

What is the purpose of the Caesar Cipher?

The Caesar Cipher is used for basic encryption and learning purposes in cryptography. It helps understand the concept of substitution ciphers.

How do you break a Caesar Cipher?

Since it has only 25 possible shifts, brute force decryption is easy. Frequency analysis of letters also helps in breaking the cipher.

Can the Caesar Cipher be used for real-world security?

No, because it is too easy to break. More advanced encryption techniques like AES and RSA are used for secure communication.

Conclusion

In this article, we discussed how to implement the Caesar Cipher program in Java, a simple encryption technique that shifts characters by a fixed number of positions in the alphabet. This method is widely used in basic cryptography and data security. Understanding the Caesar Cipher helps in learning encryption techniques, string manipulation, and character encoding in Java.

Live masterclass