0

I have been trying to setup a simple openssl_seal and openssl_open script in PHP (8.0.8 is my Version installed), I don't get any errors but openssl_open always returns FALSE.

Can anyone tell me if I did something wrong?

$algo = 'aes-256-gcm';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($algo));
$data = "Data to Seal";

$res = openssl_pkey_new(array(
    'digest_alg'        => 'sha256',
    'private_key_bits'  => 2048,
    'private_key_type'  => OPENSSL_KEYTYPE_RSA
));

openssl_pkey_export($res, $privkey); 
    
$pubkey = openssl_pkey_get_details($res);
$pubkey = $pubkey["key"];

openssl_seal($data, $sealed, $envelope, array($pubkey), $algo, $iv);

$openKey = openssl_pkey_get_private($privkey);

if (openssl_open($sealed, $open, $envelope[0], $openKey, $algo, $iv)) {
    echo "OPENED: ".$open;
} else {
    echo "Failed to open Data!";
}    
Sunny
  • 125
  • 9
  • I’ve been playing with this for a while, and can only get it to work with `aes256`, not `aes-256-gcm`. Also, `$iv` is a ref parameter to seal, and as such whatever you set it to is thrown away, at least as far as I can tell, and the function generates its own value. You still want to capture it to pass to open, however. I’d also encourage you to check out the more documented Sodium encryption functions: https://www.php.net/manual/en/function.sodium-crypto-box-seal.php – Chris Haas Oct 17 '21 at 15:52
  • Thanks for the help, I tried it before with different algorithms, but it didn't help, now using `aes256` works fine. About the `$iv`, if I don't set it, I get an Error `openssl_seal(): Argument #6 ($iv) cannot be null for the chosen cipher algorithm` so you need to set it to something at least. I will have a look at Sodium next, but at least for the project I'm trying to use it on the openssl version should be fine now. – Sunny Oct 18 '21 at 04:21

0 Answers0