3v4l.org

run code in 300+ PHP versions simultaneously
<?php # --- ENCRYPTION --- # the key should be random binary, use scrypt, bcrypt or PBKDF2 to # convert a string into a key # key is specified using hexadecimal $key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029f"); # show key size use either 16, 24 or 32 byte keys for AES-128, 192 # and 256 respectively $key_size = strlen($key); echo "Key size: " . $key_size . "\n"; $plaintext = "This string was AES-256 / CBC / ZeroBytePadding encrypted."; # create a random IV to use with CBC encoding $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); # creates a cipher text compatible with AES (Rijndael block size = 128) # to keep the text confidential # only suitable for encoded input that never ends with value 00h # (because of default zero padding) $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv); # prepend the IV for it to be available for decryption $ciphertext = $iv . $ciphertext; # encode the resulting cipher text so it can be represented by a string $ciphertext_base64 = base64_encode($ciphertext); echo $ciphertext_base64 . "\n"; # === WARNING === # Resulting cipher text has no integrity or authenticity added # and is not protected against padding oracle attacks. # --- DECRYPTION --- $ciphertext_dec = base64_decode($ciphertext_base64); # retrieves the IV, iv_size should be created using mcrypt_get_iv_size() $iv_dec = substr($ciphertext_dec, 0, $iv_size); # retrieves the cipher text (everything except the $iv_size in the front) $ciphertext_dec = substr($ciphertext_dec, $iv_size); # may remove 00h valued characters from end of plain text $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec); echo $plaintext_dec . "\n"; ?>
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.6
Key size: 21 Fatal error: Uncaught Error: Call to undefined function mcrypt_get_iv_size() in /in/Lk2qT:17 Stack trace: #0 {main} thrown in /in/Lk2qT on line 17
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Key size: 21 Fatal error: Call to undefined function mcrypt_get_iv_size() in /in/Lk2qT on line 17
Process exited with code 255.
Output for 4.4.5 - 4.4.9
Key size: 21 Fatal error: Call to undefined function: mcrypt_get_iv_size() in /in/Lk2qT on line 17
Process exited with code 255.
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.4
Key size: 21 Fatal error: Call to undefined function: mcrypt_get_iv_size() in /in/Lk2qT on line 17
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Key size: 21 Fatal error: Call to undefined function: mcrypt_get_iv_size() in /in/Lk2qT on line 17

preferences:
329.59 ms | 401 KiB | 459 Q