Secret Message Program in Java

Creating a secret message program in Java can be an interesting project. In this article, you will learn a simple example of a secret message program that encrypts and decrypts a message using a simple Caesar cipher.

Caesar cipher:

The Caesar cipher is one of the simplest and earliest encryption techniques used to secure messages. It’s a substitution cipher where each letter in the plaintext is shifted a certain number of positions down or up the alphabet.

Named after Julius Caesar, who is believed to have used it to communicate confidential information, the Caesar cipher works by replacing each letter in the plaintext with a letter that is a fixed number of positions (known as the “key” or “shift”) away in the alphabet. If the shift is 3, for example, then ‘A’ would be replaced by ‘D’, ‘B’ would become ‘E’, and so on.

Here’s an overview of how the Caesar cipher works:

  1. Key Generation:
    • Choose a shift value, often denoted as “k”, which represents the number of positions each letter in the plaintext will be shifted.
    • The shift can be any integer between 1 and 25, as there are 26 letters in the English alphabet.
  2. Encryption:
    • For each letter in the plaintext message:
      • If the letter is uppercase (‘A’ to ‘Z’), replace it with the letter that is “k” positions down the alphabet.
      • If the letter is lowercase (‘a’ to ‘z’), replace it with the letter that is “k” positions down the lowercase alphabet.
      • Non-alphabetic characters (such as spaces, digits, and punctuation) are left unchanged.
  3. Decryption:
    • To decrypt the message, apply the reverse process:
      • Shift each letter “k” positions up the alphabet for uppercase and lowercase letters.
      • Non-alphabetic characters remain unchanged.

For example, let’s use a Caesar cipher with a shift of 3:

  • Plaintext: “HELLO WORLD”
  • Encryption:
    • ‘H’ shifted 3 positions becomes ‘K’
    • ‘E’ shifted 3 positions becomes ‘H’
    • ‘L’ shifted 3 positions becomes ‘O’
    • ‘O’ shifted 3 positions becomes ‘R’
    • … and so on.
  • Encrypted Message: “KHOOR ZRUOG”

To decrypt the message, simply shift each letter 3 positions up the alphabet:

  • Encrypted Message: “KHOOR ZRUOG”
  • Decryption:
    • ‘K’ shifted 3 positions up becomes ‘H’
    • ‘H’ shifted 3 positions up becomes ‘E’
    • ‘O’ shifted 3 positions up becomes ‘L’
    • ‘R’ shifted 3 positions up becomes ‘O’
    • … and so on.
  • Decrypted Message: “HELLO WORLD”

Note:

While the Caesar cipher is easy to understand and implement, it’s not secure for modern cryptographic purposes due to its simplicity and vulnerability to brute-force attacks. However, it serves as a useful introduction to encryption concepts and historical cryptographic techniques.

Java secret message program example

package com.javacodepoint.miscellaneous;

import java.util.Scanner;
/*
 * Simple example of a secret message program that 
 * encrypts and decrypts a message using a simple Caesar cipher.
 */
public class SecretMessageProgram {
	
	public static String encrypt(String message, int shift) {
		StringBuilder encryptedMessage = new StringBuilder();

		for (char ch : message.toCharArray()) {
			if (Character.isLetter(ch)) {
				char base = Character.isUpperCase(ch) ? 'A' : 'a';
				char encryptedChar = (char) (base + (ch - base + shift) % 26);
				encryptedMessage.append(encryptedChar);
			} else {
				encryptedMessage.append(ch);
			}
		}

		return encryptedMessage.toString();
	}

	public static String decrypt(String encryptedMessage, int shift) {
		return encrypt(encryptedMessage, 26 - shift);
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter a message: ");
		String message = scanner.nextLine();

		System.out.print("Enter a shift value (0-25): ");
		int shift = scanner.nextInt();

		String encryptedMessage = encrypt(message, shift);
		System.out.println("Encrypted Message: " + encryptedMessage);

		String decryptedMessage = decrypt(encryptedMessage, shift);
		System.out.println("Decrypted Message: " + decryptedMessage);

		scanner.close();
	}
}

OUTPUT:

Enter a message: hello world!
Enter a shift value (0-25): 3
Encrypted Message: khoor zruog!
Decrypted Message: hello world!

Explanation of the program:

  • The encrypt method takes a message and a shift value as inputs and returns the encrypted message using the Caesar cipher.
  • The decrypt method takes an encrypted message and a shift value as inputs and returns the decrypted message.
  • The main method reads a message and a shift value from the user, then encrypts and decrypts the message using the provided shift value.
  • The encrypt method shifts only alphabetical characters while leaving non-alphabetical characters unchanged.

See also: Generating OTP in Java | Generate Random String in Java

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

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