3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Define a 32-byte (64 character) hexadecimal encryption key // Note: The same encryption key used to encrypt the data must be used to decrypt the data define('ENCRYPTION_KEY', 'd0a7e7997b6d5fcd55f4b5c32611b87cd923e88837b63bf2941ef819dc8ca282'); // Encrypt Function function mc_encrypt($encrypt, $key){ $encrypt = serialize($encrypt); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM); $key = pack('H*', $key); $mac = hash_hmac('sha256', $encrypt, substr(bin2hex($key), -32)); $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt.$mac, MCRYPT_MODE_CBC, $iv); $encoded = base64_encode($passcrypt).'|'.base64_encode($iv); return $encoded; } // Decrypt Function function mc_decrypt($decrypt, $key){ $decrypt = explode('|', $decrypt.'|'); $decoded = base64_decode($decrypt[0]); $iv = base64_decode($decrypt[1]); if(strlen($iv)!==mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)){ return false; } $key = pack('H*', $key); $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv)); $mac = substr($decrypted, -64); $decrypted = substr($decrypted, 0, -64); $calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32)); if($calcmac!==$mac){ return false; } $decrypted = unserialize($decrypted); return $decrypted; } echo '<h1>Rijndael 256-bit CBC Encryption Function</h1>'; $data = 'Super secret confidential string data.'; $encrypted_data = mc_encrypt('pssst ... đon’t tell anyøne!', ENCRYPTION_KEY); echo '<h2>Example #1: String Data</h2>'; echo 'Data to be Encrypted: ' . $data . '<br/>'; echo 'Encrypted Data: ' . $encrypted_data . '<br/>'; echo 'Decrypted Data: ' . mc_decrypt($encrypted_data, ENCRYPTION_KEY) . '</br>'; $data = array(1, 5, 8, 9, 22, 10, 61); $encrypted_data = mc_encrypt($data, ENCRYPTION_KEY); echo '<h2>Example #2: Non-String Data</h2>'; echo 'Data to be Encrypted: <pre>'; print_r($data); echo '</pre><br/>'; echo 'Encrypted Data: ' . $encrypted_data . '<br/>'; echo 'Decrypted Data: <pre>'; print_r(mc_decrypt($encrypted_data, ENCRYPTION_KEY)); echo '</pre>'; ?>
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
<h1>Rijndael 256-bit CBC Encryption Function</h1> Fatal error: Uncaught Error: Call to undefined function mcrypt_create_iv() in /in/qZsA5:9 Stack trace: #0 /in/qZsA5(33): mc_encrypt('s:32:"pssst ......', 'd0a7e7997b6d5fc...') #1 {main} thrown in /in/qZsA5 on line 9
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <h1>Rijndael 256-bit CBC Encryption Function</h1> Fatal error: Uncaught Error: Call to undefined function mcrypt_create_iv() in /in/qZsA5:9 Stack trace: #0 /in/qZsA5(33): mc_encrypt('s:32:"pssst ......', 'd0a7e7997b6d5fc...') #1 {main} thrown in /in/qZsA5 on line 9
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
<h1>Rijndael 256-bit CBC Encryption Function</h1> Fatal error: Call to undefined function mcrypt_create_iv() in /in/qZsA5 on line 9
Process exited with code 255.

preferences:
242.21 ms | 402 KiB | 330 Q