Skip to content

Instantly share code, notes, and snippets.

@rocketeerbkw
Created May 30, 2015 03:50
Show Gist options
  • Save rocketeerbkw/6a584455c193b2d86a3f to your computer and use it in GitHub Desktop.
Save rocketeerbkw/6a584455c193b2d86a3f to your computer and use it in GitHub Desktop.
<?php
$encoded = ['x/y', 'x/y'];
$chars = [];
// Calculate the full prime number
$prime_num = gmp_sub(gmp_pow($exp, $base), 1);
$prime = gmp_strval($prime_num);
// Convert encoded message to 5-bit characters
foreach ($encoded as $word) {
list($offset, $length) = explode('/', $word);
$number = substr($prime, $offset, $length);
$number_bin = decbin($number);
$number_pad = pad_to_nearest_five($number_bin);
$number_array = str_split($number_pad, 5);
$chars = array_merge($chars, $number_array);
}
$legend = ['', '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', ' ',
'.'];
// Convert 5-bit characters to decoded message
foreach ($chars as $key => $char) {
echo $legend[bindec($char)];
if (($key+1) % 80 == 0) {
echo PHP_EOL;
}
}
echo PHP_EOL;
// Given a string, pad it with zeros so that the total string length is
// divisible by five
function pad_to_nearest_five($number) {
$length = strlen($number);
while ($length % 5 != 0) {
$length++;
}
return str_pad($number, $length, '0', STR_PAD_LEFT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment