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 = []; $iv = random_bytes(openssl_cipher_iv_length('aes-256-gcm')); $result = openssl_seal($plaintext, $sealed, $ekeys, [$pubkey], 'aes-256-gcm', $iv); 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, $iv]); } /** * 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, $iv) = 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', $iv); 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/ G71cMVc5XXgoV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C1CYtFlRct1wu+RHgrv5wKUiK7iaIxfJhO0vZm94AnfpXG7fVai /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);
based on WlUBq
Output for 7.0.0 - 7.2.0
bool(false) Warning: Invalid argument supplied for foreach() in /in/oFRLg on line 47 Warning: openssl_open(): IV length is invalid in /in/oFRLg on line 57 Fatal error: Uncaught Exception: openssl_open failed! in /in/oFRLg:59 Stack trace: #0 /in/oFRLg(108): easy_unseal(false, '-----BEGIN RSA ...', true) #1 {main} thrown in /in/oFRLg on line 59
Process exited with code 255.
Output for 5.4.15 - 5.6.28
Fatal error: Call to undefined function random_bytes() in /in/oFRLg on line 19
Process exited with code 255.
Output for 5.4.0 - 5.4.14
Fatal error: Call to undefined function openssl_get_publickey() in /in/oFRLg on line 13
Process exited with code 255.