3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(1); ini_set('display_errors', 1); class Encryption { private $key; protected $iv_size; protected $iv; public function __construct() { # --- ENCRYPTION --- # the key should be random binary, use scrypt, bcrypt or PBKDF2 to # convert a string into a key # key is specified using hexadecimal $this->key = pack("H*", "myKeyIsGreaterth2nanndbestofall04nkdsdffsd546754sdfvsdg6efflsdfs"); # create a random IV to use with CBC encoding $this->iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND); } public function encryptData($input) { $output = $this->encrypt($input); return $output; } public function decryptData($input) { $input = base64_decode($input); $output = $this->decrypt($input); return $output; } public function decrypt($string) { # retrieves the IV, iv_size should be created using mcrypt_get_iv_size() $iv_dec = substr($string, 0, $this->iv_size); # retrieves the cipher text (everything except the $iv_size in the front) $string = substr($string, $this->iv_size); # may remove 00h valued characters from end of plain text $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $string, MCRYPT_MODE_CBC, $iv_dec); return $output; } public function encrypt($string) { # 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) $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $string, MCRYPT_MODE_CBC, $this->iv); # prepend the IV for it to be available for decryption $output = $this->iv . $output; # encode the resulting cipher text so it can be represented by a string $output = base64_encode($output); # === WARNING === # Resulting cipher text has no integrity or authenticity added # and is not protected against padding oracle attacks. return $output; } } $test = new Encryption(); $encrypted = $test->encryptData("Narendra"); echo "This is encrypted text of a string Narendra $encrypted \n"; echo "This is decrypted text ".$test->decryptData($encrypted);
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.4, 8.3.6
Fatal error: Uncaught Error: Call to undefined function mcrypt_get_iv_size() in /in/6fBcN:19 Stack trace: #0 /in/6fBcN(70): Encryption->__construct() #1 {main} thrown in /in/6fBcN on line 19
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 Fatal error: Uncaught Error: Call to undefined function mcrypt_get_iv_size() in /in/6fBcN:19 Stack trace: #0 /in/6fBcN(70): Encryption->__construct() #1 {main} thrown in /in/6fBcN on line 19
Process exited with code 255.
Output for 7.0.5
This is encrypted text of a string Narendra yHqLS/oOqRxRDn7Mbhsb+GRdr/liikEYkYW65m1+1bc= This is decrypted text Narendra
Output for 7.0.4
This is encrypted text of a string Narendra TukNuo8zMFx1dxXrSr4WQxp67B9u3OkxcM73KWHms2M= This is decrypted text Narendra
Output for 7.0.3
This is encrypted text of a string Narendra NliqgKqNHNmNf/TKyyCeArG6qJqcc8rc5yZznnOkfHo= This is decrypted text Narendra
Output for 7.0.2
This is encrypted text of a string Narendra PuC5gKNle+dH6To6RgzBzjmWscYQAeVpgYkSSaJ7Ibw= This is decrypted text Narendra
Output for 7.0.1
This is encrypted text of a string Narendra 7AHMZCscfJGzS2oxynyz1rXWuumLHM6hiNHifsaKtEo= This is decrypted text Narendra
Output for 7.0.0
This is encrypted text of a string Narendra XYW/+ug4X8A/XHU1A+ZsO4AmYGoyJyFU9d5XTn9cebU= This is decrypted text Narendra
Output for 5.5.35, 5.6.21
Fatal error: Call to undefined function mcrypt_get_iv_size() in /in/6fBcN on line 19
Process exited with code 255.
Output for 5.6.20
This is encrypted text of a string Narendra SdClhafp4hsSQkm1x0WGETQAMxVyetwynNPUBNqWRGw= This is decrypted text Narendra
Output for 5.6.19
This is encrypted text of a string Narendra cO69WiVax6xwhTqeCVscuDzJmt1unvOzliT71ZIspzw= This is decrypted text Narendra
Output for 5.6.18
This is encrypted text of a string Narendra 1j2U62Ey9HF7NEh22eOXBUYDKEtgw0MnOiZfHX0ekAk= This is decrypted text Narendra
Output for 5.6.17
This is encrypted text of a string Narendra IBzXhyTABh9zIZIwAvvdRqULugTNAgnpTHFd3Dh4Y2A= This is decrypted text Narendra
Output for 5.6.16
This is encrypted text of a string Narendra D1DCaosW+Zaq0biYjFHAtKmP1uF3Z4gZ1O280OKKYo4= This is decrypted text Narendra
Output for 5.6.15
This is encrypted text of a string Narendra 33SGzujisAausbTB0lm9fk5UoofXzguXjDjstr7ai+c= This is decrypted text Narendra
Output for 5.6.14
This is encrypted text of a string Narendra asn8uc7sGbDgZz9L4dEBdpzX/8QxvSlxCuUrsvcrVko= This is decrypted text Narendra
Output for 5.6.13
This is encrypted text of a string Narendra NjJrmUH52HEC0B05UuBOnGS3baPU+SgFtcWLt1gTKww= This is decrypted text Narendra
Output for 5.6.12
This is encrypted text of a string Narendra 5ktublsYmvPWEPJxY8cJJsco0j2m9/Y518nXVLxwr7M= This is decrypted text Narendra
Output for 5.6.11
This is encrypted text of a string Narendra dxOucok3bJMuYyyuU9SPIsDA5dT7KAWDcpmhpvjbkPg= This is decrypted text Narendra
Output for 5.6.10
This is encrypted text of a string Narendra dCFeghpNryZlSt281fMUvtJNzEp6WsQlOGm9wC/Bva8= This is decrypted text Narendra
Output for 5.6.9
This is encrypted text of a string Narendra g3DpftwK71MUrF3qDBSwE6Wurbkbm1qF9tURgASzvJI= This is decrypted text Narendra
Output for 5.6.8
This is encrypted text of a string Narendra arpwvjEgeI24YtrhB3O17HWT0aVER6MN9b673kYab/g= This is decrypted text Narendra
Output for 5.5.34
This is encrypted text of a string Narendra L7pOmLcauBgoncj6L7Xs0z0/7AGYyJOyI6feQV/8C4o= This is decrypted text Narendra
Output for 5.5.33
This is encrypted text of a string Narendra abCxVf11NqHtIng/QZwoVZIVTxKpVg1l9v4HwVpBs5w= This is decrypted text Narendra
Output for 5.5.32
This is encrypted text of a string Narendra VIoZPSzVIT+ymZUiMSdDQtXOK+3ekcYoUy+1jhr1Jks= This is decrypted text Narendra
Output for 5.5.31
This is encrypted text of a string Narendra 6ECdtIecQn2icG9gQxzCFJZ9yZO1hmlO9W4TCzDk6oA= This is decrypted text Narendra
Output for 5.5.30
This is encrypted text of a string Narendra KKj+TWtfkM+WPzVJzrLEhx7pGSJWzwFBDK2OC1cgfBc= This is decrypted text Narendra
Output for 5.5.29
This is encrypted text of a string Narendra w10O/akEHWxbHkAZ2nedmuv3RmowfG8nelPJBnDyqNo= This is decrypted text Narendra
Output for 5.5.28
This is encrypted text of a string Narendra CA2tuVVo2l4ZvT21mjpGAXLX2gSE3b06rwLezueAnSg= This is decrypted text Narendra
Output for 5.5.27
This is encrypted text of a string Narendra 2vEKoJoIKT2WiMRnW5gDiS8G7D65zg0hmwefmTaeHKI= This is decrypted text Narendra
Output for 5.5.26
This is encrypted text of a string Narendra ACrSD14XdrfCxv58Dhe9Za+eowt/rNfmobq3XALK49E= This is decrypted text Narendra
Output for 5.5.25
This is encrypted text of a string Narendra SPxQ0qjVQ1J3gkrl/qDUlEfhNmbsxkTYQ5z/Lr9dpfs= This is decrypted text Narendra
Output for 5.5.24
This is encrypted text of a string Narendra qJJQqKKUbqHqvisPTdR9575C8WbDFgIK4elw0n9yFxg= This is decrypted text Narendra

preferences:
180.05 ms | 401 KiB | 196 Q