Atbash Cipher

Java Examples

Below are some examples of how to implement the Atbash Cipher in Java.

Java Example


public class AtbashCipher {
    
    // Encryption and decryption methods
    public static String transform(String text) {
        StringBuilder result = new StringBuilder();
        for (char c : text.toCharArray()) {
            if (Character.isLetter(c)) {
                char base = Character.isUpperCase(c) ? 'A' : 'a';
                result.append((char) (25 - (c - base) + base));
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }

    // test
    public static void main(String[] args) {
        String plaintext = "HELLO";
        String ciphertext = transform(plaintext);
        String decryptedText = transform(ciphertext);

        System.out.println("Plaintext: " + plaintext);
        System.out.println("Ciphertext: " + ciphertext);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

            

Detailed Tutorials

Check out our detailed tutorials on how to implement the Atbash Cipher in different programming languages.