3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * A human-usable variant of openssl_seal() * * @param string $plaintext Your message * @param string $publickey_string PEM-encoded RSA public key * @param boolean $encode Hex-encode the output? * * @return string */ function easy_seal($plaintext, $publickey_string, $encode = false) { $pubkey = openssl_get_publickey($publickey_string); if ($pubkey === false) { throw new Exception('Could not load public key'); } $sealed = ''; $ekeys = []; $result = openssl_seal($plaintext, $sealed, $ekeys, [$pubkey], 'aes-256-gcm'); if ($result === false) { throw new Exception('openssl_seal failed!'); } if ($encode) { $sealed = bin2hex($sealed); foreach ($ekeys as $i => $key) { $ekeys[$i] = bin2hex($key); } } return json_encode([$sealed, $ekeys]); } /** * Inverse operation of easy_seal() * * @param string $ciphertext (the output of easy_seal()) * @param string $privatekey_string PEM-encoded RSA private key * @param boolean $encoded Do we need to decode from hex? * * @return string */ function easy_unseal($ciphertext, $privatekey_string, $encoded = false) { list($sealed, $ekeys) = json_decode($ciphertext, true); if ($encoded) { $sealed = hex2bin($sealed); foreach ($ekeys as $i => $key) { $ekeys[$i] = hex2bin($key); } } $open_data = ''; $privkey = openssl_get_privatekey($privatekey_string); if ($privkey === false) { throw new Exception('Could not load public key'); } $result = openssl_open($sealed, $open_data, $ekeys, $privkey, 'aes-256-gcm'); if ($result === false) { throw new Exception('openssl_open failed!'); } return $open_data; } $private_key_pem = "-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAtCY43TsWfw+Ep8FiU0Z/DiivoyHAFzckFttOgBh9wZMI+zYy XAmvmbpFh6EsfAn1zcZAJNIjb7jXt9ySD8/gTHSvVRhawysSk+jvG7bVNy+JmEcb B0bIFLUVud1sZWJANv999iub1Rpzb6xIhZ5ZsEDMob4UWxouKPfrRQcYlrI3X7d/ VLfmnU/sSzpS6eHn+f064zXXmf1tVIBwEKkq/eaxgJyW8AM8XL5qL9RueHhihQm/ G71cMVc5XXgoVy8+egx/aQm2u26d9ez1Vu804ptJoLAnjTdx1an5CKFikpvt2OYP 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ai /fuoTPkCgYEA0pxWFSyEOeQt1LV3rTZGxK6QAt5h36JYWt8/ck2YrMeZn2wAv+hy ZgtWsdrJLvEVId3wH79vCQtAjZJAyCVxAhXPlAhxmA8F3LOqEgwrnls52SmWw/5T sVlqPCXEO3fNZLOd5sYYmiaQjWbP4dAP5WQU75UhYAnyz6Myz3rbN58= -----END RSA PRIVATE KEY-----"; $public_key_pem = "-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtCY43TsWfw+Ep8FiU0Z/ DiivoyHAFzckFttOgBh9wZMI+zYyXAmvmbpFh6EsfAn1zcZAJNIjb7jXt9ySD8/g THSvVRhawysSk+jvG7bVNy+JmEcbB0bIFLUVud1sZWJANv999iub1Rpzb6xIhZ5Z sEDMob4UWxouKPfrRQcYlrI3X7d/VLfmnU/sSzpS6eHn+f064zXXmf1tVIBwEKkq /eaxgJyW8AM8XL5qL9RueHhihQm/G71cMVc5XXgoVy8+egx/aQm2u26d9ez1Vu80 4ptJoLAnjTdx1an5CKFikpvt2OYPZVFuWV6cqMMCksqzBSZKMu7wtaiEc/UN41UH cQIDAQAB -----END PUBLIC KEY-----"; $plaintext = 'Something something dark side'; $store_me = easy_seal($plaintext, $public_key_pem, true); var_dump($store_me); $visible = easy_unseal($store_me, $private_key_pem, true); var_dump($visible);
Output for 7.1.20, 7.2.6
Fatal error: Uncaught Error: Call to undefined function openssl_get_publickey() in /in/WlUBq:13 Stack trace: #0 /in/WlUBq(103): easy_seal('Something somet...', '-----BEGIN PUBL...', true) #1 {main} thrown in /in/WlUBq on line 13
Process exited with code 255.
Output for 7.0.0 - 7.1.10, 7.2.0
Warning: openssl_seal(): Cipher algorithm requires an IV to be supplied as a sixth parameter in /in/WlUBq on line 19 Fatal error: Uncaught Exception: openssl_seal failed! in /in/WlUBq:21 Stack trace: #0 /in/WlUBq(103): easy_seal('Something somet...', '-----BEGIN PUBL...', true) #1 {main} thrown in /in/WlUBq on line 21
Process exited with code 255.
Output for 5.6.14 - 5.6.28
Warning: openssl_seal(): Ciphers with modes requiring IV are not supported in /in/WlUBq on line 19 Fatal error: Uncaught exception 'Exception' with message 'openssl_seal failed!' in /in/WlUBq:21 Stack trace: #0 /in/WlUBq(103): easy_seal('Something somet...', '-----BEGIN PUBL...', true) #1 {main} thrown in /in/WlUBq on line 21
Process exited with code 255.
Output for 5.4.15 - 5.6.13

Process exited with code 139.
Output for 5.4.0 - 5.4.14
Fatal error: Call to undefined function openssl_get_publickey() in /in/WlUBq on line 13
Process exited with code 255.