3v4l.org

run code in 300+ PHP versions simultaneously
<?php function encrypt_decrypt($action, $string) { $output = false; $encrypt_method = "AES-256-CBC"; $secret_key = 'This is my secret key'; $secret_iv = 'This is my secret iv'; // hash $key = hash('sha256', $secret_key); // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning $iv = substr(hash('sha256', $secret_iv), 0, 16); if( $action == 'encrypt' ) { $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv); $output = base64_encode($output); } else if( $action == 'decrypt' ){ $output = mcrypt_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv); } return $output; } $plain_txt = "This is my plain text"; echo "Plain Text = $plain_txt\n"; $encrypted_txt = encrypt_decrypt('encrypt', $plain_txt); echo "Encrypted Text = $encrypted_txt\n"; $decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt); echo "Decrypted Text = $decrypted_txt\n"; if( $plain_txt === $decrypted_txt ) echo "SUCCESS"; else echo "FAILED"; echo "\n";
Output for 7.0.6 - 7.0.20, 7.1.0 - 7.1.25, 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.6
Plain Text = This is my plain text Fatal error: Uncaught Error: Call to undefined function mcrypt_encrypt() in /in/eMCvj:17 Stack trace: #0 /in/eMCvj(31): encrypt_decrypt('encrypt', 'This is my plai...') #1 {main} thrown in /in/eMCvj on line 17
Process exited with code 255.
Output for 7.0.0 - 7.0.5
Plain Text = This is my plain text Notice: Undefined variable: plaintext in /in/eMCvj on line 17 Warning: mcrypt_encrypt(): Key of size 64 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in /in/eMCvj on line 18 Encrypted Text = Warning: mcrypt_decrypt(): Module initialization failed in /in/eMCvj on line 22 Decrypted Text = FAILED
Output for 5.5.35, 5.6.21 - 5.6.28
Plain Text = This is my plain text Fatal error: Call to undefined function mcrypt_encrypt() in /in/eMCvj on line 17
Process exited with code 255.
Output for 5.6.7 - 5.6.20
Plain Text = This is my plain text Notice: Undefined variable: plaintext in /in/eMCvj on line 18 Warning: mcrypt_encrypt(): Key of size 64 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in /in/eMCvj on line 18 Encrypted Text = Warning: mcrypt_decrypt(): Module initialization failed in /in/eMCvj on line 22 Decrypted Text = FAILED
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.34
Plain Text = This is my plain text Notice: Undefined variable: plaintext in /in/eMCvj on line 18 Warning: mcrypt_encrypt(): Size of key is too large for this algorithm in /in/eMCvj on line 18 Encrypted Text = rkJun6TtDSNJXbm+RP5ZGA== Warning: mcrypt_decrypt(): Module initialization failed in /in/eMCvj on line 22 Decrypted Text = FAILED

preferences:
204.49 ms | 402 KiB | 242 Q