Java Secret Message Program

Java Secret Message Program | Java Secret message uses the Caesar cipher algorithm. This program works on coding and decoding. Caesar cipher algorithm is an encryption algorithm that encrypts the given words. Also see:- Monty Hall Java Program

The working of the caesar cipher algorithm is as follows:-

Each letter is encoded by shifting each letter by a given amount of number that is called a key suppose the key is 2 then each of the characters in the given message is shifted by 3 digits from its position. For example:-

Consider the message:- “Java Programming Language”
Let the key be 3 so, K = 3
Now the encrypted message will be:-
Encrypted message:- “mdyd surjudpplqj odqjxdjh”

Here J -> m that is the third letter from ‘J’ is ‘m’, a -> d the third letter from ‘a’ is ‘d’ similarly for all the letters it has been encoded.

Consider another example:-
Secret message:- “Know Program”
Secret key (K) = 8
Encrypted message:- “svwe xzwoziu”

Java Secret Message Program

Now let us the Java Secret Message Program. We will take the string and key from the end-user. To take input from the end-user we will use the Scanner class.

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner scan = new Scanner(;

      System.out.print("Secret Message: ");
      String message = scan.nextLine();
      message = message.toLowerCase();

      System.out.print("Secret key: ");
      int key = scan.nextInt();

      System.out.print("The encoded message: " 
                       + encode(message, key));

   public static String encode(String text, int shift) {
      String result = "";
      for (int i = 0; i < text.length(); i++) {
         char letter = text.charAt(i);
         if (letter >= 'a' && letter <= 'z') {
            letter = (char) (letter + shift);
            if (letter > 'z') {
               letter = (char) (letter - 26);
            } else if (letter < 'a') {
               letter = (char) (letter + 26);
         result = result + letter;
      return result;


Secret Message: Trust Me, I’m a “Programmer”.
Secret key: 3
The encoded message: wuxvw ph, l’p d “surjudpphu”.

Secret Message: “The only way to learn a new programming language is by writing programs in it.”
Secret key: 5
The encoded message: “ymj tsqd bfd yt qjfws f sjb uwtlwfrrnsl qfslzflj nx gd bwnynsl uwtlwfrx ns ny.”

The actual working of the code is in the method encode() which takes a string and integer arguments of the text and the shift.

Step-1: Get each character from the secret message by using the toCharAt() method of the Java String class.

Step-2: Then if the letter is greater than ‘a’ and lesser than ‘z’ then add the letter to the shift again. If the letter is greater than ‘z’ then subtract the letter by 26 or else if the letter is lesser than ‘a’ then add the letter to 26. Return the result.

Step-3: Call the encode() method and print the required statements in the main method.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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