Implement Caesar Cipher in Java

Hey guys! The topic for today is how to implement Caesar Cipher in Java.
So we will learn how to encode our message using Caesar Cipher algorithm which is both simplest and easiest of all encryption algorithms.

Caesar Cipher

Caesar Cipher is named after Julius Caesar and is one of the simplest and weakest encryption algorithms.
Therefore it is used only in parts of other complex encryption algorithms making the CipherText harder to decode.

Caesar cipher or Shift Cipher is a Substitution cipher algorithm in which each letter of the plain text (message) is substituted with another letter. In this algorithm, each letter of the Plaintext is shifted a number of positions based on the Key provided.
For example:
PlainText: Hello!
Key: 3
Each letter of the plain text is shifted three times to the next letter.

  • H -> K
  • e -> h
  • l -> o
  • l -> o
  • o -> r
  • ! -> !

Therefore the CipherText is: Khoor!

Point to be Noted: The cipher only encrypts letters; symbols, numbers remain unencrypted.

 

Code to implement Caesar Cipher in Java

import java.util.*;
class CaesarCipher{
    public static void main(String[]args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter your PlainText: ");
        String message = scan.next();
        int length = message.length();
        System.out.print("Enter the Key: ");
        int key = scan.nextInt();

        String uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lowercase = "abcdefghijklmnopqrstuvwxyz";
        String special = "!#$%&'()*+,-./:;<=>[email protected][]^_`{|}~";
        String numbers = "0123456789";
        System.out.print("The encrypted Text is: ");
        for(int i = 0 ;i < length ;i++)
        {
            for(int j = 0; j < 26 ;j++)
            {
                if(j < special.length() && message.charAt(i) == special.charAt(j))
                 {
                     System.out.print(special.charAt(j)); //print special charecters as it is
                 }
                else if(j < numbers.length() && message.charAt(i) == numbers.charAt(j))
                {
                    System.out.print(numbers.charAt(j)); //print numbers as it is
                }
                else if(message.charAt(i) == lowercase.charAt(j))
                {
                    System.out.print(lowercase.charAt((j + key) % 26));
                }
                else if(message.charAt(i) == uppercase.charAt(j))
                {
                    System.out.print(uppercase.charAt((j + key) % 26));
                }          
            }
        }
        System.out.println();
    }
}

Explanation of Caesar Cipher Java Program

  1. We check if the input string consists of any special characters or numbers. If so, we print them as it is.
  2. If we encounter a Lowercase or an Uppercase letter we add the value of the key to the ASCII value of that letter and print it.
  3. We perform modulo 26 operations as there are 26 alphabets. So if the shift takes to the ending alphabets we are reverted back to the beginning alphabets. (i.e) in case of a shift by 3, w, x, y and z would map to z, a, b and c.

Output:

Implement Caesar Cipher in Java

Advantages:

  1. Simple and Easy to implement.
  2. Uses a single Key.
  3. Requires very few system resources.

Disadvantages:

  1. Minimum Security.
  2. Frequency of letter pattern gives out the clue in deciphering the message.

Hope you’ve understood the code 🙂
Any questions please feel free to drop in your comments.

Learn more:

You can also check out my other posts at:
https://www.codespeedy.com/author/k_preetham/

Leave a Reply

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