Home » PHP scripts » Atbash PHP encoder / decoder

The Atbash Cipher was originally a monoalphabetic substitution cipher used for the Hebrew alphabet. It is one of the earliest known subtitution ciphers to have been used, and is very simple. However, it’s simplicity is also it’s biggest pitfall, as it does not use a key. Hence every piece of plaintext enciphered using the Atbash Cipher uses the same ciphertext alphabet, and so can be easily broken, since the encryption algorithm is known to all.

The Atbash Cipher simply reverses the plaintext alphabet to create the ciphertext alphabet. That is, the first letter of the alphabet is encrypted to the last letter of the alphabet, the second letter to the penultimate letter and so forth. In the original Hebrew this means that ‘aleph’ is encrypted to ‘tav’, and ‘beth’ to ‘shin’. This is where we get the name of the cipher ‘atbash’.

You can find out more information about Atbash on Wikipedia.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
function atbash($string)
{
    $string = preg_replace("/[^a-z]/", "", strtolower($string));
    $final = "";
    $v = array(
    "a"=>"z",
    "b"=>"y",
    "c"=>"x",
    "d"=>"w",
    "e"=>"v",
    "f"=>"u",
    "g"=>"t",
    "h"=>"s",
    "i"=>"r",
    "j"=>"q",
    "k"=>"p",
    "l"=>"o",
    "m"=>"n",
    "n"=>"m",
    "o"=>"l",
    "p"=>"k",
    "q"=>"j",
    "r"=>"i",
    "s"=>"h",
    "t"=>"g",
    "u"=>"f",
    "v"=>"e",
    "w"=>"d",
    "x"=>"c",
    "y"=>"b",
    "z"=>"a"
    );
 
    for($i=0;$i<strlen($string);$i++)
    {
        $final .= $v[$string[$i]];
    }
 
    return $final;
}

echo atbash("Your message");
?>

Leave a Reply

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

*
*