Caesar Cipher in Java (Encryption and Decryption)

Here you will get program for caesar cipher in Java for encryption and decryption.

Caesar Cipher is an encryption algorithm in which each alphabet present in plain text is replaced by alphabet some fixed number of positions down to it. Take below example.

Plain Text: ABCD

Key: 3

Cipher Text (Encrypted Message): DEFG

As key is 3 so each alphabet will be replaced by an alphabet 3 places down to it.

Caesar Cipher in Java (Encryption and Decryption)

To decrypt a cipher text, the reverse of encryption process is followed.

Also Read: Hill Cipher in Java

Also Read: Java Vigenere Cipher

Below I have shared the program to implement this algorithm in Java.

Program for Caesar Cipher in Java

Encryption

import java.util.Scanner;

public class CaesarCipherJava {
	public static void main(String...s){
		String message, encryptedMessage = "";
		int key;
		char ch;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter a message: ");
		message = sc.nextLine();
		
		System.out.println("Enter key: ");
		key = sc.nextInt();

		for(int i = 0; i < message.length(); ++i){
			ch = message.charAt(i);
			
			if(ch >= 'a' && ch <= 'z'){
	            ch = (char)(ch + key);
	            
	            if(ch > 'z'){
	                ch = (char)(ch - 'z' + 'a' - 1);
	            }
	            
	            encryptedMessage += ch;
	        }
	        else if(ch >= 'A' && ch <= 'Z'){
	            ch = (char)(ch + key);
	            
	            if(ch > 'Z'){
	                ch = (char)(ch - 'Z' + 'A' - 1);
	            }
	            
	            encryptedMessage += ch;
	        }
	        else {
	        	encryptedMessage += ch;
	        }
		}
		
		System.out.println("Encrypted Message = " + encryptedMessage);
	}
}

Output

Enter a message:
Azk nMQ ls
Enter key:
3
Encrypted Message = Dcn qPT ov

Decryption

import java.util.Scanner;

public class CaesarCipherJava {
	public static void main(String...s){
		String message, decryptedMessage = "";
		int key;
		char ch;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter a message: ");
		message = sc.nextLine();
		
		System.out.println("Enter key: ");
		key = sc.nextInt();

		for(int i = 0; i < message.length(); ++i){
			ch = message.charAt(i);
			
			if(ch >= 'a' && ch <= 'z'){
	            ch = (char)(ch - key);
	            
	            if(ch < 'a'){
	                ch = (char)(ch + 'z' - 'a' + 1);
	            }
	            
	            decryptedMessage += ch;
	        }
	        else if(ch >= 'A' && ch <= 'Z'){
	            ch = (char)(ch - key);
	            
	            if(ch < 'A'){
	                ch = (char)(ch + 'Z' - 'A' + 1);
	            }
	            
	            decryptedMessage += ch;
	        }
	        else {
	        	decryptedMessage += ch;
	        }
		}
		
		System.out.println("Decrypted Message = " + decryptedMessage);
	}
}

Output

Enter a message:
abz gpQ
Enter key:
2
Decrypted Message = yzx enO

Comment below if you have any doubts related to above program for caesar cipher in Java.

8 thoughts on “Caesar Cipher in Java (Encryption and Decryption)”

  1. Can you send me a detailed description of the coding for me to understand ever line. It will be very helpful if possible. thank you.

Leave a Comment

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