<?php
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 512,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the private and public key
$res = openssl_pkey_new($config);
// Extract the private key from $res to $privKey
openssl_pkey_export($res, $privKey);
// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
$data = 'plaintext data goes here';
// Encrypt the data to $encrypted using the public key
openssl_public_encrypt($data, $encrypted, $pubKey);
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, $privKey);
echo $decrypted;
Fatal error: Uncaught Error: Undefined constant "OPENSSL_KEYTYPE_RSA" in /in/VMLuf:5
Stack trace:
#0 {main}
thrown in /in/VMLuf on line 5
Process exited with code 255.
Warning: Use of undefined constant OPENSSL_KEYTYPE_RSA - assumed 'OPENSSL_KEYTYPE_RSA' (this will throw an Error in a future version of PHP) in /in/VMLuf on line 5
Fatal error: Uncaught Error: Call to undefined function openssl_pkey_new() in /in/VMLuf:9
Stack trace:
#0 {main}
thrown in /in/VMLuf on line 9
Process exited with code 255.
Output for 7.0.20, 7.1.5 - 7.1.10, 7.2.0
Warning: openssl_pkey_export(): cannot get key from parameter 1 in /in/VMLuf on line 12
Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in /in/VMLuf on line 15
Warning: openssl_public_encrypt(): key parameter is not a valid public key in /in/VMLuf on line 21
Warning: openssl_private_decrypt(): key parameter is not a valid private key in /in/VMLuf on line 24
Output for 7.1.20 - 7.1.33
Notice: Use of undefined constant OPENSSL_KEYTYPE_RSA - assumed 'OPENSSL_KEYTYPE_RSA' in /in/VMLuf on line 5
Fatal error: Uncaught Error: Call to undefined function openssl_pkey_new() in /in/VMLuf:9
Stack trace:
#0 {main}
thrown in /in/VMLuf on line 9
Process exited with code 255.
Notice: Use of undefined constant OPENSSL_KEYTYPE_RSA - assumed 'OPENSSL_KEYTYPE_RSA' in /in/VMLuf on line 5
Fatal error: Call to undefined function openssl_pkey_new() in /in/VMLuf on line 9
Process exited with code 255.