3v4l.org

run code in 300+ PHP versions simultaneously
<?php class AESEncryptExampleClass { public function _encrypt($plaintext, $passphrase, $keySize, $salt, $iv, $iterationCount) { $mode = MCRYPT_MODE_CBC; //Generate AES encryption key $key = $this->_pbkdf2($passphrase, "e84ad660c4721ae0e84ad660c4721ae0", $iterationCount, $this->_parseKeyInt(MCRYPT_RIJNDAEL_128)); echo base64_encode($key) . "\n"; try { $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $this->_pkcs5Pad($plaintext), $mode, $iv); } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } return (base64_encode($ciphertext)); } public function _getRandomBytes($length = 8) { $characters = '0123456789'; $charactersLength = strlen($characters) - 1; $bytes = ''; //Select some random characters for ($i = 0; $i < $length; $i++) { $bytes .= $characters[mt_rand(0, $charactersLength)]; } return $bytes; } private function _pbkdf2($passphrase, $salt, $iterationCount = 1000, $keyLength = MCRYPT_RIJNDAEL_128, $algorithm = 'sha1') { $hashLength = strlen( hash( $algorithm, null, true ) ); $blockCount = ceil( $keyLength / $hashLength ); $output = ''; //Create key for ( $block = 1; $block <= $blockCount; $block++ ) { //Initial hash for this block $xorsum = $last = hash_hmac( $algorithm, $salt . pack( 'N', $block ), $passphrase, true ); //Perform block iterations for ( $i = 1; $i < $iterationCount; $i++ ) { //XOR each iterate $xorsum ^= ( $last = hash_hmac( $algorithm, $last, $passphrase, true ) ); } //Append iterated block $output .= $xorsum; } //Return derived key of correct length return substr( $output, 0, $keyLength ); } private function _parseKeyInt($keySize) { $key = ""; if ($keySize == MCRYPT_RIJNDAEL_128) { $key = 16; } else if ($keySize == MCRYPT_RIJNDAEL_192) { $key = 24; } else if ($keySize == MCRYPT_RIJNDAEL_256) { $key = 32; } return $key; } private function _pkcs5Pad($text) { $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $padding = $blockSize - (strlen($text) % $blockSize); $text .= str_repeat(chr($padding), $padding); return $text; } } /*-----------------------------------------------------------* * EXAMPLE USING AESEncryptExampleClass * *-----------------------------------------------------------*/ $iterationCount = 1000; $keySize = 128; $youmapsKey = "YOUR_YOUMAPS_KEY"; $url = "exampleURL"; //Initialize AESEncryptExampleClass $aesEncrypt = new AESEncryptExampleClass(); //Generate random IV and salt $salt = "1234567891234567"; $iv = "1234567891234567"; //$salt = $aesEncrypt->_getRandomBytes(16); //$iv = $aesEncrypt->_getRandomBytes(16); //Encrypt the URL $encryptedURL = $aesEncrypt->_encrypt($url, $youmapsKey, $keySize, $salt, $iv, $iterationCount); echo $encryptedURL; ?>

preferences:
39.6 ms | 402 KiB | 5 Q