RiptideTempora

Test script for AES-256-CTR in PHP

Jan 4th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. <?
  2. function convBase($numberInput, $fromBaseInput, $toBaseInput) {
  3.   // FROM PHP.NET         [email protected]
  4.   if ($fromBaseInput==$toBaseInput) return $numberInput;
  5.     $fromBase = str_split($fromBaseInput,1);
  6.     $toBase = str_split($toBaseInput,1);
  7.     $number = str_split($numberInput,1);
  8.     $fromLen=strlen($fromBaseInput);
  9.     $toLen=strlen($toBaseInput);
  10.     $numberLen=strlen($numberInput);
  11.     $retval='';
  12.     if($toBaseInput == '0123456789') {
  13.       $retval=0;
  14.       for($i = 1; $i <= $numberLen; $i++) {
  15.         $retval = bcadd($retval,
  16.                     bcmul(array_search($number[$i-1], $fromBase), bcpow($fromLen,$numberLen-$i))
  17.                   );
  18.       }
  19.       return $retval;
  20.     }
  21.     if($fromBaseInput != '0123456789') {
  22.       $base10 = convBase($numberInput, $fromBaseInput, '0123456789');
  23.     }
  24.     else
  25.     $base10 = $numberInput;
  26.     if ($base10<strlen($toBaseInput)) {
  27.       return $toBase[$base10];
  28.     }
  29.     while($base10 != '0')
  30.     {
  31.       $retval = $toBase[bcmod($base10,$toLen)].$retval;
  32.       $base10 = bcdiv($base10,$toLen,0);
  33.     }
  34.   return $retval;
  35. }
  36. function binToFullkey($raw) {
  37.   return convBase(bin2hex($raw), '0123456789abcdef',
  38.         '!"#$%^\'()+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~');
  39.   // All printable ASCII characters
  40. }
  41. function randomChars($len) {
  42.   // 94-character keyspace
  43.   $l = ceil($len * 5 / 6);
  44.   return substr(binToFullkey(openssl_random_pseudo_bytes($l)), 0, $len);
  45. }
  46. error_reporting(E_ALL);
  47. ini_set('display_errors', E_ALL|E_NOTICE|E_STRICT);
  48. header("Content-Type: text/plain");
  49. function AES_256_CTR_Encrypt($plain, $key, $IV) {
  50.   return trim(
  51.            base64_encode(
  52.              mcrypt_encrypt(
  53.                MCRYPT_RIJNDAEL_256,
  54.                $key,
  55.                $plain,
  56.                'ctr',
  57.                $IV
  58.              )
  59.            )
  60.          );
  61. }
  62. function AES_256_CTR_Decrypt($cipher, $key, $IV) {
  63.   return trim(
  64.            mcrypt_decrypt(
  65.              MCRYPT_RIJNDAEL_256,
  66.              $key,
  67.              base64_decode($cipher),
  68.              'ctr',
  69.              $IV
  70.            )
  71.          );
  72. }
  73. ################################################################################
  74. # Testing AES-256-CTR in PHP                                                   #
  75. ################################################################################
  76. $plain = randomChars(1024);
  77. $key = openssl_random_pseudo_bytes(32);
  78. $IV = openssl_random_pseudo_bytes(32);
  79. echo "AES-256-CTR Mode test!\n";
  80. echo "Plaintext:\n\t".chunk_split($plain, 40, "\n\t")."\n";
  81. echo "Key: ".base64_encode($key)."\n";
  82. echo "IV: ".base64_encode($IV)."\n";
  83. echo "======================================================================\n";
  84. echo "Encrypted:\n\t";
  85. $ctext = AES_256_CTR_Encrypt($plain, $key, $IV);
  86. echo chunk_split($ctext, 40, "\n\t");
  87. echo "\nDecrypted:\n\t";
  88. $ptext = AES_256_CTR_Decrypt($ctext, $key, $IV);
  89. echo chunk_split($ptext, 40, "\n\t");
  90. echo "\nNo fatal errors? Hooray!";
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment