Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- function convBase($numberInput, $fromBaseInput, $toBaseInput) {
- // FROM PHP.NET [email protected]
- if ($fromBaseInput==$toBaseInput) return $numberInput;
- $fromBase = str_split($fromBaseInput,1);
- $toBase = str_split($toBaseInput,1);
- $number = str_split($numberInput,1);
- $fromLen=strlen($fromBaseInput);
- $toLen=strlen($toBaseInput);
- $numberLen=strlen($numberInput);
- $retval='';
- if($toBaseInput == '0123456789') {
- $retval=0;
- for($i = 1; $i <= $numberLen; $i++) {
- $retval = bcadd($retval,
- bcmul(array_search($number[$i-1], $fromBase), bcpow($fromLen,$numberLen-$i))
- );
- }
- return $retval;
- }
- if($fromBaseInput != '0123456789') {
- $base10 = convBase($numberInput, $fromBaseInput, '0123456789');
- }
- else
- $base10 = $numberInput;
- if ($base10<strlen($toBaseInput)) {
- return $toBase[$base10];
- }
- while($base10 != '0')
- {
- $retval = $toBase[bcmod($base10,$toLen)].$retval;
- $base10 = bcdiv($base10,$toLen,0);
- }
- return $retval;
- }
- function binToFullkey($raw) {
- return convBase(bin2hex($raw), '0123456789abcdef',
- '!"#$%^\'()+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~');
- // All printable ASCII characters
- }
- function randomChars($len) {
- // 94-character keyspace
- $l = ceil($len * 5 / 6);
- return substr(binToFullkey(openssl_random_pseudo_bytes($l)), 0, $len);
- }
- error_reporting(E_ALL);
- ini_set('display_errors', E_ALL|E_NOTICE|E_STRICT);
- header("Content-Type: text/plain");
- function AES_256_CTR_Encrypt($plain, $key, $IV) {
- return trim(
- base64_encode(
- mcrypt_encrypt(
- MCRYPT_RIJNDAEL_256,
- $key,
- $plain,
- 'ctr',
- $IV
- )
- )
- );
- }
- function AES_256_CTR_Decrypt($cipher, $key, $IV) {
- return trim(
- mcrypt_decrypt(
- MCRYPT_RIJNDAEL_256,
- $key,
- base64_decode($cipher),
- 'ctr',
- $IV
- )
- );
- }
- ################################################################################
- # Testing AES-256-CTR in PHP #
- ################################################################################
- $plain = randomChars(1024);
- $key = openssl_random_pseudo_bytes(32);
- $IV = openssl_random_pseudo_bytes(32);
- echo "AES-256-CTR Mode test!\n";
- echo "Plaintext:\n\t".chunk_split($plain, 40, "\n\t")."\n";
- echo "Key: ".base64_encode($key)."\n";
- echo "IV: ".base64_encode($IV)."\n";
- echo "======================================================================\n";
- echo "Encrypted:\n\t";
- $ctext = AES_256_CTR_Encrypt($plain, $key, $IV);
- echo chunk_split($ctext, 40, "\n\t");
- echo "\nDecrypted:\n\t";
- $ptext = AES_256_CTR_Decrypt($ctext, $key, $IV);
- echo chunk_split($ptext, 40, "\n\t");
- echo "\nNo fatal errors? Hooray!";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment