<?php
ini_set('display_errors', 1);
$config = array(
"config" => "C:\wamp\bin\apache\Apache2.4.4\conf\openssl.cnf",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the private and public key
$res = openssl_pkey_new($config);
if ($res === false) die('Failed to generate key pair.'."\n");
if (!openssl_pkey_export($res, $privKey, "phrase", $config)) die('Failed to retrieve private key.'."\n");
// Extract the private key from $res to $privKey
openssl_pkey_export($res, $privKey, "phrase", $config);
echo "<br/>";
echo "Private Key = ".$privKey;
echo "<br/>";
// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
echo "<br/>";
echo "Public Key = ".$pubKey;
echo "<br/>";
$data = 'plaintext data goes here';
// Encrypt the data to $encrypted using the public key
openssl_public_encrypt($data, $encrypted, $pubKey);
echo "<br/>";
echo "Encrypted Data = ".$encrypted;
echo "<br/>";
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, $privKey);
echo "<br/>";
echo "Decrypted Data = ".$decrypted;
echo "<br/>";
?>
LOGS
Private Key = -----BEGIN ENCRYPTED PRIVATE KEY----- MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIr2acPfh8YYQCAggA MBQGCCqGSIb3DQMHBAiCvohdiWAZ4QSCBMjKJUXF5ShKfW3TazpKYTxEV8JmGYLf AJWXzxdi0GrDuddz4aW1FeGwvUm2t/41CTxFsWtgoQJrzCgAQETn54majdrDeF4u zCmvFMKSoVP4xsZKke15e1K1LPmFNNuKKyCqMwL+tpQJ7zquvDTKHapUnNzfNXpZ D2K1r2qZWeDN1d36DA9wkN5GbpZYAjuHqHUNzorhxIbHGp2WOg8YKdemoTuKIqYC DUKncWtxRUOx6IIZuey+uTBzH7Bn9K9a71QTjUdeWgQZFzy9yVpetB+XrJA92IWt vMeKXCXNhOgkOvkUPNXSuMOVrECNcbKDAKxmK3EQWqb+8zlYFqjmaL/sCep8ihio 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CXPjwBMehiFjOMhzSLW7xQj Qg2KcilfW6oKFzDQQ5nKPEXvQYMhQ1MeWKyNv6BMoc4EEpIGhtziUXWhgT4sN1ES 5sxVcGVoIe1viO/kk3Zq55hETlZbNWs3V511BcEZCiQNrntnbYv6pwKpoB21ZV2E slVhYcslEGliIQKQsWSl5cfc+pqjLteiPrwk14WKJGXl9zX3YH6H7KKB/7SIRZk7 wq8= -----END ENCRYPTED PRIVATE KEY-----
Public Key = -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAriS8qflAjYSYhH2qgC5T yf98X1qoLMXIW9mMkhV8LcApBKOfNjUMc9xjD3a8CR/LYwa4MYhevoKcVfPG8XoE sDGyHh+h/vtYP0rORB1T3RULVUzDLjX558e2KqPrSN+rV+Jl1NB0SO5Of3JA+AKa 0Q3botcjOM3WuFa/s+RzsiCrMMhzOZSTBj+GTP/VcDipF5PM7+/Lxr+edjRXccT2 WQjsq0sUrtsmpzBE8Niph361RjfIisxKoksQGs7hC/Iv4yhBzZZIpRaZuvDj4ImP +4sUQgbdVVCso122kg34UtY5qchuSCcJfsGbD2zMw+8ZftIsJ7dfX1FxujgggDyn 0wIDAQAB -----END PUBLIC KEY-----
Encrypted Data = Uš6/ùÅËæÝmL4²G¾'gr¨Ñĉï‚zêbÀ)[îR0s‹yÝ`t™õ°ÞeÏd>×o¯rß9ÌÔÅAü!-†D·¨ÎVZ¼?¶éžäýöaØT~=‚Fan¢ºq{M”ƒ¹Cû5N3¹.Ð(·#*ÏRƹñß÷õƒ_ò9c-Ÿ% ×óè2Ꙃõ“ÂÐgNÈ-ˆd«…ºt§¼Ô}yŠ"7èPš(¶R¤ßJÚ_h¶ðÞK(Cj“7‘Y ÀŠþrôZƒ4)JU•˜„üˆ k0â§Êë^ÚºGÚªúVKø†ë8ÏLÚó „Ÿ¦¿¤
( ! ) Warning: openssl_private_decrypt(): key parameter is not a valid private key in C:\wamp\www\android\pki_example.php on line 41
Call Stack
# Time Memory Function Location
1 0.0020 252696 {main}( ) ..\pki_example.php:0
2 0.2043 258032 openssl_private_decrypt ( ) ..\pki_example.php:41
Decrypted Data =
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, openssl_pkey_get_private($privKey, "phrase"));
echo "<br/>";
echo "Decrypted Data = ".$decrypted;
echo "<br/>";
openssl_private_decrypt
function is capable to use PEM formatted private key but your key is encrypted and this function does not have an argument for password. You have to use openssl_pkey_get_private
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With