Caesar Cipher in Java

Sheeraz Gul Oct 12, 2023
  1. Caesar Cipher in Java
  2. Caesar Cipher Encryption in Java
  3. Caesar Cipher Decryption in Java
Caesar Cipher in Java

Caesar Cipher is one of the simplest methods for performing encryption. This tutorial demonstrates how to perform encryption and decryption using Caesar Cipher in Java.

Caesar Cipher in Java

Caesar Cipher is of the earliest approaches for performing encryption; it can be implemented in any programming language. The algorithm is simple; for example, if we perform encryption on the text delftstack, the Caesar Cipher algorithm will encrypt the text by replacing the characters with the fixed position below the characters in alphabetic order.

We pass a key which is an integer, to decide how many positions we want to go down in encrypting the characters.

For example, if the text is ABCD and the key is 2, the encrypted text will be CDEF. Let’s try to implement the Caesar Cipher encryption approach in Java.

Caesar Cipher Encryption in Java

The code below demonstrates the implementation of Caesar Cipher encryption in Java.

package delftstack;

import java.util.Scanner;

public class Caesar_Cipher_Encrypt {
  public static void main(String... s) {
    String Original_Message, Encrypted_Message = "";
    int Cipher_Key;
    char Message_Char;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter a message: ");
    Original_Message = sc.nextLine();
    System.out.println("Enter the key: ");
    Cipher_Key = sc.nextInt();

    for (int i = 0; i < Original_Message.length(); ++i) {
      Message_Char = Original_Message.charAt(i);
      if (Message_Char >= 'a' && Message_Char <= 'z') {
        Message_Char = (char) (Message_Char + Cipher_Key);

        if (Message_Char > 'z') {
          Message_Char = (char) (Message_Char - 'z' + 'a' - 1);
        }

        Encrypted_Message += Message_Char;
      } else if (Message_Char >= 'A' && Message_Char <= 'Z') {
        Message_Char = (char) (Message_Char + Cipher_Key);

        if (Message_Char > 'Z') {
          Message_Char = (char) (Message_Char - 'Z' + 'A' - 1);
        }

        Encrypted_Message += Message_Char;
      } else {
        Encrypted_Message += Message_Char;
      }
    }
    System.out.println("The Encrypted Message is : " + Encrypted_Message);
  }
}

The code above implements the Caesar Cipher encryption method. See the output:

Please enter a message:
This is delftstack.com
Enter the key:
3
The Encrypted Message is : Wklv lv ghoiwvwdfn.frp

Caesar Cipher Decryption in Java

Decryption will be the vice versa operation. See the implementation of decryption using the Caesar Cipher method.

package delftstack;

import java.util.Scanner;

public class Caesar_Cipher_Decrypt {
  public static void main(String... s) {
    String Original_Message, Decrypted_Message = "";
    int Cipher_Key;
    char Message_Char;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter an encrypted message: ");
    Original_Message = sc.nextLine();
    System.out.println("Enter the key: ");
    Cipher_Key = sc.nextInt();

    for (int i = 0; i < Original_Message.length(); ++i) {
      Message_Char = Original_Message.charAt(i);
      if (Message_Char >= 'a' && Message_Char <= 'z') {
        Message_Char = (char) (Message_Char - Cipher_Key);

        if (Message_Char < 'a') {
          Message_Char = (char) (Message_Char + 'z' - 'a' + 1);
        }

        Decrypted_Message += Message_Char;
      } else if (Message_Char >= 'A' && Message_Char <= 'Z') {
        Message_Char = (char) (Message_Char - Cipher_Key);

        if (Message_Char < 'A') {
          Message_Char = (char) (Message_Char + 'Z' - 'A' + 1);
        }

        Decrypted_Message += Message_Char;
      } else {
        Decrypted_Message += Message_Char;
      }
    }
    System.out.println("The Decrypted Message is : " + Decrypted_Message);
  }
}

The code above implements the Caesar Cipher decryption in Java. See the output:

Please enter an encrypted message:
Wklv lv ghoiwvwdfn.frp
Enter the key:
3
The Decrypted Message is : This is delftstack.com
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Encryption