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.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
Fatal error: Uncaught Error: Call to undefined function mcrypt_get_iv_size() in /in/P3Mor:19 Stack trace: #0 /in/P3Mor(70): Encryption->__construct() #1 {main} thrown in /in/P3Mor 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/P3Mor:19 Stack trace: #0 /in/P3Mor(70): Encryption->__construct() #1 {main} thrown in /in/P3Mor on line 19
Process exited with code 255.
Output for 7.0.5
This is encrypted text of a string Narendra5SHD2D7kDF9KZEUwgtmB2rHU99KGoCPMpAQOb+wOHE4= This is decrypted text Narendra
Output for 7.0.4
This is encrypted text of a string Narendranc4QUdEEQq9OgZwEBh1qUEg3oljYEJTdnBwAhid36Ko= This is decrypted text Narendra
Output for 7.0.3
This is encrypted text of a string NarendraZ7ll/nkCSL/1mGyEQgHDBTN2jxTzsqegxs3BSdwgDuM= This is decrypted text Narendra
Output for 7.0.2
This is encrypted text of a string Narendra68iT8jxYrWMlI3gasWu+XZOxu5SeX4FzT01ZlH9ByDA= This is decrypted text Narendra
Output for 7.0.1
This is encrypted text of a string NarendraMeEETTopPUPCm/Fy52vqEap/rEIcQF+1bIlpd0Fu86o= This is decrypted text Narendra
Output for 7.0.0
This is encrypted text of a string NarendrawXUU3fx2pyHscZ5ogVbGvjR83nvLLt1PEydSJLX8A1Q= This is decrypted text Narendra
Output for 5.5.35, 5.6.21 - 5.6.28
Fatal error: Call to undefined function mcrypt_get_iv_size() in /in/P3Mor on line 19
Process exited with code 255.
Output for 5.6.20
This is encrypted text of a string NarendraFFmQ8vIwlbomV/IMOGjRm/CxHdQHC6Fs2U9ND7gOiSE= This is decrypted text Narendra
Output for 5.6.19
This is encrypted text of a string NarendraZz858qNCplNT6Rsu4Hbbi5rEEil9z2BqFMrrHfXTyqw= This is decrypted text Narendra
Output for 5.6.18
This is encrypted text of a string NarendraZSFbqM/vhObveTiEnNLSIHDM1lnwV9AVy4rs1BDBwFw= This is decrypted text Narendra
Output for 5.6.17
This is encrypted text of a string Narendra3JPFJcLWcfugjFee6T9oxo3oq+wsZU0X7l7RQwcwhTI= This is decrypted text Narendra
Output for 5.6.16
This is encrypted text of a string NarendrabKYoj8AU0Gv8tfXWVbhoAs7qvx0FUq0v+k1hhOJnmVk= This is decrypted text Narendra
Output for 5.6.15
This is encrypted text of a string Narendray9dej4SE6QvHjBthCztv1ZCMLlNS29TcJWY040JWpsI= This is decrypted text Narendra
Output for 5.6.14
This is encrypted text of a string Narendra749tW4HrVWeQLVGSpCLbPiqPHtmA6aJoQAGl+BJbW9o= This is decrypted text Narendra
Output for 5.6.13
This is encrypted text of a string Narendra/lMfqHHMEa5IG47LlmSTM8CntSoAoASSmE0gtgpqpxk= This is decrypted text Narendra
Output for 5.6.12
This is encrypted text of a string NarendraBn9FlexNAqwvwiwH8ZRxWNOHznUwSFqrrijKTVi5120= This is decrypted text Narendra
Output for 5.6.11
This is encrypted text of a string NarendraV+2C9ORYLOaN15wYHlMoIVIZt+tcQZEFvY4WECHe+t4= This is decrypted text Narendra
Output for 5.6.10
This is encrypted text of a string NarendraicjaRP1BharMwuhcZ1313lD8N8spKgWqEiqMX7+Bm60= This is decrypted text Narendra
Output for 5.6.9
This is encrypted text of a string Narendra9ULTXZ1vkwN3ur+iZzFOUSOaZ4/rP38Te/DYUYzHLhg= This is decrypted text Narendra
Output for 5.6.8
This is encrypted text of a string NarendraOPoACZI3gUoztOQdb/mFgTdSmbrM4GbxcZoaRf0507c= This is decrypted text Narendra
Output for 5.5.34
This is encrypted text of a string NarendrarQCcLqs1qlLRjyaaomF6apsq02bvtn5GVXLtL136vP4= This is decrypted text Narendra
Output for 5.5.33
This is encrypted text of a string NarendraGJFWXz2ucLcJvUSjDzKQW/564Cu2VuW+jgJUyxYXoto= This is decrypted text Narendra
Output for 5.5.32
This is encrypted text of a string NarendracVE13AryKU5C2P6QM/rOMcoY+t3M1y1pgpPnfzKWBzs= This is decrypted text Narendra
Output for 5.5.31
This is encrypted text of a string NarendraKQGaWaMBQqkx4Kc+2alg44Q9J0ZXPLWHwhEr0bUZW50= This is decrypted text Narendra
Output for 5.5.30
This is encrypted text of a string NarendrazGaRXAGwwyXbS3yQ7ycFUvDaw1L5rRFdT8q02jfax7g= This is decrypted text Narendra
Output for 5.5.29
This is encrypted text of a string NarendraF5YUQesz9M/APEHV70lAVrA/atyeMRCohDFcfWV8T8s= This is decrypted text Narendra
Output for 5.5.28
This is encrypted text of a string NarendraY1MY0mrii3MRCseoOjW21jQM9U1s8yIFmvB8NDaEzMI= This is decrypted text Narendra
Output for 5.5.27
This is encrypted text of a string NarendraFOTR2xgdVMSzDCSFNpXUjN3tnhS53CRXSt50o7x5/yQ= This is decrypted text Narendra
Output for 5.5.26
This is encrypted text of a string Narendra6PQOngvbKHOeIkpvdZxn41EPbeFvY4yPKmS643gxY1M= This is decrypted text Narendra
Output for 5.5.25
This is encrypted text of a string NarendraIzg+ZB2ISKC/df1CNx6AjNnhACUkG9ctx7LoR2ceaOo= This is decrypted text Narendra
Output for 5.5.24
This is encrypted text of a string Narendra6+340jgduUn1LMlgpXbhB1gnQBpozo6zNn/zd4vcbCQ= This is decrypted text Narendra

preferences:
154.91 ms | 401 KiB | 213 Q