无法解密数据 - openssl_private_decrypt(): 密钥参数不是有效的私钥

Unable to decrypt data - openssl_private_decrypt(): key parameter is not a valid private key

<?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/>";
?>

日志

Private Key = -----BEGIN ENCRYPTED PRIVATE KEY----- MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIr2acPfh8YYQCAggA MBQGCCqGSIb3DQMHBAiCvohdiWAZ4QSCBMjKJUXF5ShKfW3TazpKYTxEV8JmGYLf AJWXzxdi0GrDuddz4aW1FeGwvUm2t/41CTxFsWtgoQJrzCgAQETn54majdrDeF4u zCmvFMKSoVP4xsZKke15e1K1LPmFNNuKKyCqMwL+tpQJ7zquvDTKHapUnNzfNXpZ D2K1r2qZWeDN1d36DA9wkN5GbpZYAjuHqHUNzorhxIbHGp2WOg8YKdemoTuKIqYC DUKncWtxRUOx6IIZuey+u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jIjS VvI73eULzc3j0jIptWxzrHWM6iHx1zRxkLMJSZOx0A27ngtSo7g6+aJnMO5FDfdR 90vnr+bX4ki+X/N4wVF7eppyapLe/tQ54vAxsyIBrCXPjwBMehiFjOMhzSLW7xQj 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 函数能够使用 PEM 格式的私钥,但您的密钥已加密且此函数没有密码参数。您必须改用 openssl_pkey_get_private

不知道是什么原因。我认为有人可以用确切的理由编辑它。

当我在一行中使用私钥时,出现了上述问题中提到的错误。 但是当我通过将它分成几行 (每行 64 个字符) 来使用相同的私钥时,它起作用了。这样就不需要使用openssl_pkey_get_private($privKey, "phrase")函数了。其实我的私钥没有密码。

请像下面的代码一样使用

  // Create the keypair
    $privatefile = "data/privatekey.pem";
    $publicfile = "data/publickey.pem";
    $cleartextfile = "data/cleartext";
    $res = openssl_pkey_new();
    if (!$res) {
        echo "\nopenssl_pkey_new: " . openssl_error_string();
    }

    // Get private key
    openssl_pkey_export($res, $privatekey);
    if (!$privatekey) {
        echo "\nopenssl_pkey_export: " . openssl_error_string();
    }
    file_put_contents($privatefile, $privatekey);

    // Get public key
    $pkeydetails = openssl_pkey_get_details($res);
    if (!$pkeydetails) {
        echo "\nopenssl_pkey_get_details: " . openssl_error_string();
    }
    $publickey = $pkeydetails["key"];
    file_put_contents($publicfile, $publickey);
  // For getting Decrypted password
   $postedPassord= $_POST['password'];
    openssl_private_decrypt(base64_decode($postedPassord), 
     $decryptPassword, $privatekey);