Atbash Cipher

Programming Examples

Below are some examples of how to implement the Atbash Cipher in various programming languages.

Python Example


def atbash_cipher(text):
    result = ''
    for char in text:
        if char.isalpha():
            offset = 65 if char.isupper() else 97
            result += chr(25 - (ord(char) - offset) + offset)
        else:
            result += char
    return result

plaintext = "HELLO"
ciphertext = atbash_cipher(plaintext)
print(f'Plaintext: {plaintext}')
print(f'Ciphertext: {ciphertext}')
            

JavaScript Example


function atbashCipher(text) {
    return text.split('').map(char => {
        if (/[a-zA-Z]/.test(char)) {
            const base = char <= 'Z' ? 65 : 97;
            return String.fromCharCode(25 - (char.charCodeAt(0) - base) + base);
        }
        return char;
    }).join('');
}

const plaintext = "HELLO";
const ciphertext = atbashCipher(plaintext);
console.log(`Plaintext: ${plaintext}`);
console.log(`Ciphertext: ${ciphertext}`);
            

Detailed Tutorials

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