Han

Han Ferik

Home | Projects | AP CSA

Caesar Cipher

Tools: VSCode | Source Code: GitHub

This Java program implements a basic Caesar cipher, a simple encryption technique where each letter in the input is shifted by a fixed number of positions in the alphabet. Specifically, this program shifts each letter forward by three places (e.g., 'a' → 'd', 'b' → 'e', etc.). The program takes user input, encrypts it by shifting the letters, and then prints the transformed text.

The CeaserCipher class is designed to handle user input and perform the encryption process. The constructor method (CeaserCipher) uses a Scanner object to prompt the user for input and stores the entered text in the input variable. Once the input is received, the Scanner is closed to prevent resource leaks.

The encryption is performed by the CeaserReturner method. This method processes the input string in reverse order, checking each character and shifting it forward by three places if it is a letter. If the character is not a letter, it remains unchanged. The equalsIgnoreCase method is used to ensure that the encryption works regardless of letter case. The transformed text is stored in the newinput variable, which is returned as the encrypted result.

The program's main method initializes an instance of CeaserCipher, calls the CeaserReturner method to perform the encryption, and prints the resulting encrypted text. However, the program's approach is inefficient due to the extensive use of if-else statements for character conversion. A more optimized approach would involve using ASCII values or direct char manipulation to achieve the shift in a simpler and more efficient way.


Caesar Cipher in Java

Caesar Cipher in Java


        import java.util.Scanner;
    
        public class CaesarCipher {
            private String input;
            private String newinput = "";
    
            public CaesarCipher() {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter Input? ");
                input = scanner.nextLine();
                scanner.close();
            }
    
            public String CeaserReturner() {
                for (int i = input.length(); i > 0; i--) {
                    char ch = input.charAt(i - 1);
                    if (Character.isLetter(ch)) {
                        char shifted = (char) (ch + 3);
                        if ((Character.isLowerCase(ch) && shifted > 'z') || 
                            (Character.isUpperCase(ch) && shifted > 'Z')) {
                            shifted -= 26;
                        }
                        newinput += shifted;
                    } else {
                        newinput += ch;
                    }
                }
                return newinput;
            }
    
            public static void main(String[] args) {
                CaesarCipher cipher = new CaesarCipher();
                String result = cipher.CeaserReturner();
                System.out.println("New Text: " + result);
            }
        }
        

Find me on the interwebs!

Github LinkedIn Instagram Facebook