/** * @dataProvider continuousBufferCombos */ public function testEncryptDecryptWithContinuousBuffer($mode, $plaintext, $iv, $key) { $aes = new Crypt_AES(constant($mode)); $aes->enableContinuousBuffer(); $aes->setIV($iv); $aes->setKey($key); $actual = ''; for ($i = 0, $strlen = strlen($plaintext); $i < $strlen; ++$i) { $actual .= $aes->decrypt($aes->encrypt($plaintext[$i])); } $this->assertEquals($plaintext, $actual); }
function random($len) { if (CRYPT_IS_WINDOWS) { if (function_exists('openssl_random_pseudo_bytes') && version_compare(PHP_VERSION, '5.3.4', '>=')) { return openssl_random_pseudo_bytes($len); } // Looks like mcrypt_create_iv with MCRYPT_DEV_RANDOM is still // unreliable on 5.3.6: // https://bugs.php.net/bug.php?id=52523 if (function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.7', '>=')) { return mcrypt_create_iv($len); } } else { if (function_exists('openssl_random_pseudo_bytes')) { return openssl_random_pseudo_bytes($len); } static $fp = null; if ($fp == null) { $fp = @fopen('/dev/urandom', 'rb'); } if ($fp) { return fread($fp, $len); } if (function_exists('mcrypt_create_iv')) { return mcrypt_create_iv($len, MCRYPT_DEV_URANDOM); } } $seed = session_id() . microtime() . getmypid(); $key = pack('H*', sha1($seed . 'A')); $iv = pack('H*', sha1($seed . 'C')); $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR); $crypto->setKey($key); $crypto->setIV($iv); $crypto->enableContinuousBuffer(); //Sliding iv. $start = mt_rand(5, PHP_INT_MAX); $output = ''; for ($i = $start; strlen($output) < $len; $i++) { $output .= $crypto->encrypt($i); } return substr($output, 0, $len); }