Last active
January 12, 2018 03:34
-
-
Save elazar/f539b8909490e85ed2471f4b87f223bf to your computer and use it in GitHub Desktop.
Jefferson cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function jefferson(string $message) | |
{ | |
$letters = array_merge(range('a', 'z'), ['_']); | |
$numbers = range(1, 26); | |
$cipher = []; | |
foreach ($numbers as $number) { | |
$cipher[$number] = []; | |
foreach ($letters as $index => $letter) { | |
$from = $letters[($index + $number) % count($letters)]; | |
$to = $letter; | |
$cipher[$number][$from] = $to; | |
} | |
} | |
$offset = 1; | |
$deciphered = ''; | |
foreach (str_split($message) as $letter) { | |
$lowered = strtolower($letter); | |
if (!isset($cipher[$offset][$lowered])) { | |
$deciphered .= $letter; | |
continue; | |
} | |
$deciphered .= $cipher[$offset][$lowered]; | |
$offset = max(1, ($offset + 1) % 27); | |
} | |
return $deciphered; | |
} | |
echo jefferson('Hthey pvf rx nmzc. Ku rix bh qebu ng vki tiliw, csue ufurk huxdbfa ndgdr anpkq fp nrsb he cgga wjugnvu ws xkl.'), PHP_EOL; | |
// great joy in camp. we are in view of the ocean, this great pacific ocean which we been so long anxious to see. | |
echo jefferson('Ujh vtgyqwq y_vft brvx vs odb u_wgv fwkhsrxr __ gwu hfvds ndlpdT (cv m x_wxxbp) ynl qu ywtky zeprhoewpc.'), PHP_EOL; | |
// the roaring noise made by the waves breaking on the rocky shores (as i suppose) may be heard distinctly. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment