Cracking the Cryptex with PHP

In the book “the Davinci Code” there was a fun little device called a cryptex.. A Cryptex has 6 dials with 6  letters on each, and only one combination producing a word will open it. The most straight forward way to crack it would be to try every combination one by one, but there’s a substantial number of combinations and we know only those which a valid words is a candidate.

With PHP, a few nested loops and the aspell interface available, cracking the code is simple matter:

$pspell\_link = pspell\_new("da"); // Bring in the dictionarry

$array1 = Array("K", "M", "A", "D", "T", "J");  // First dial
$array2 = Array("B", "H", "R", "W", "Y", "E");
$array3 = Array("S","O","L","F","V","C");
$array4 = Array("X","T","N","U","Z","P");
$array5 = Array("E","D","Z","T","G","I");
$array6 = Array("N","G","L","D","Q","K");

foreach ($array1 as $a1) {
    foreach ($array2 as $a2) {
        foreach ($array3 as $a3) {
            foreach ($array4 as $a4) {
                foreach ($array5 as $a5) {
                    foreach ($array6 as $a6) {
                        $word = $a1 . $a2 . $a3 . $a4 . $a5 . $a6;
                        if (pspell\_check($pspell\_link,$word)) { // is the word in the dictionary?
                            echo $word;
                            echo "<br>";
                        }
                    }
                }
            }
        }
    }
}

exit();

This script prints out a list of all valid (Danish) words, that can occur with the given combination of letters.