Created
January 22, 2017 00:50
-
-
Save jamiehoward/f9f1784969f0b1f6aae954a7d4aa1b1a to your computer and use it in GitHub Desktop.
One Time Pad
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 | |
$ls = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; | |
$ks = strtoupper('horsebounceauburnrooseveltbeepswapnoreastersatchelcuriousdunamiswintermintrubikdandermystifyapogeefranciscandiminish'); | |
$vs = []; | |
function encode($str) { | |
$str = strtoupper(preg_replace('/[^a-zA-Z]/', '', $str)); | |
global $ls, $ks; | |
$n = []; | |
foreach (str_split($str) as $k => $c) { | |
$n[] = $ls[(array_search($ks[$k],$ls)+array_search($c,$ls))%25]; | |
} | |
return implode('', $n); | |
} | |
function decode($str) { | |
$str = strtoupper(preg_replace('/[^a-zA-Z]/', '', $str)); | |
global $ls, $ks; | |
$n = []; | |
foreach (str_split($str) as $k => $c) { | |
$num = (array_search($c,$ls)-array_search($ks[$k],$ls))%25; | |
if ($num < 0): | |
$num = 25 - ($num*-1); | |
endif; | |
$n[] = $ls[$num]; | |
} | |
return implode('', $n); | |
} | |
if ( array_key_exists(1,$argv)): | |
if ( ! array_key_exists(2,$argv)): | |
die('String required.'); | |
endif; | |
switch ($argv[1]) { | |
case 'encode': | |
$output = encode($argv[2]); | |
break; | |
case 'decode': | |
$output = decode($argv[2]); | |
break; | |
default: | |
die('Invalid command.'); | |
} | |
echo $output . "\n"; | |
else: | |
die('Command required. Try `encode` or `decode`.'); | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment