3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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; ?>
based on eC7n8
Output for 7.2.0
Warning: Use of undefined constant MCRYPT_MODE_CBC - assumed 'MCRYPT_MODE_CBC' (this will throw an Error in a future version of PHP) in /in/t98ae on line 5 Warning: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' (this will throw an Error in a future version of PHP) in /in/t98ae on line 8 Warning: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' (this will throw an Error in a future version of PHP) in /in/t98ae on line 61 Uc7rXofKfKDeBP4oMAwXEw== Fatal error: Uncaught Error: Call to undefined function mcrypt_encrypt() in /in/t98ae:13 Stack trace: #0 /in/t98ae(101): AESEncryptExampleClass->_encrypt('exampleURL', 'YOUR_YOUMAPS_KE...', 128, '123456789123456...', '123456789123456...', 1000) #1 {main} thrown in /in/t98ae on line 13
Process exited with code 255.
Output for 7.0.6 - 7.1.7
Notice: Use of undefined constant MCRYPT_MODE_CBC - assumed 'MCRYPT_MODE_CBC' in /in/t98ae on line 5 Notice: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' in /in/t98ae on line 8 Notice: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' in /in/t98ae on line 61 Uc7rXofKfKDeBP4oMAwXEw== Fatal error: Uncaught Error: Call to undefined function mcrypt_encrypt() in /in/t98ae:13 Stack trace: #0 /in/t98ae(101): AESEncryptExampleClass->_encrypt('exampleURL', 'YOUR_YOUMAPS_KE...', 128, '123456789123456...', '123456789123456...', 1000) #1 {main} thrown in /in/t98ae on line 13
Process exited with code 255.
Output for 5.5.0 - 5.5.34, 5.6.0 - 5.6.20, 7.0.0 - 7.0.5
Uc7rXofKfKDeBP4oMAwXEw== C2khLIHyvINtqzU5gSPq3g==
Output for 5.5.35 - 5.5.38, 5.6.21 - 5.6.28
Notice: Use of undefined constant MCRYPT_MODE_CBC - assumed 'MCRYPT_MODE_CBC' in /in/t98ae on line 5 Notice: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' in /in/t98ae on line 8 Notice: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' in /in/t98ae on line 61 Uc7rXofKfKDeBP4oMAwXEw== Fatal error: Call to undefined function mcrypt_encrypt() in /in/t98ae on line 13
Process exited with code 255.