5

我试图用 PHP 加载由 openssl cli 工具生成的私钥。我使用了以下命令和 PHP 代码:

openssl genrsa -des3 4096 -out private.key

if (!($key = openssl_pkey_get_private("file://private.key", "password")));
{
      return false;
}

我确定密码是正确的,并且文件也是 PEM 格式的,但它一直返回错误。我究竟做错了什么?

在此先感谢,乔里。

4

3 回答 3

7

请注意,file://path/to/file.pem在文档中表示文件协议 + 文件路径。在 UNIX 之类的操作系统中,这类似于file:///rsa_private_key.pem. 路径字符串中有三个斜杠,而不是两个。并且file://不能省略。

于 2015-07-30T22:49:17.247 回答
4

将密钥放在 var 中要容易得多:

$public = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfmlc2EgrdhvakQApmLCDOgP0n
NERInBheMh7J/r5aU8PUAIpGXET/8+kOGI1dSYjoux80AuHvkWp1EeHfMwC/SZ9t
6rF4sYqV5Lj9t32ELbh2VNbE/7QEVZnXRi5GdhozBZtS1gJHM2/Q+iToyh5dfTaA
U8bTnLEPMNC1h3qcUQIDAQAB
-----END PUBLIC KEY-----";

$private = "-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDfmlc2EgrdhvakQApmLCDOgP0nNERInBheMh7J/r5aU8PUAIpG
XET/8+kOGI1dSYjoux80AuHvkWp1EeHfMwC/SZ9t6rF4sYqV5Lj9t32ELbh2VNbE
/7QEVZnXRi5GdhozBZtS1gJHM2/Q+iToyh5dfTaAU8bTnLEPMNC1h3qcUQIDAQAB
AoGAcbh6UFqewgnpGKIlZ89bpAsANVckv1T8I7QT6qGvyBrABut7Z8t3oEE5r1yX
UPGcOtkoRniM1h276ex9VtoGr09sUn7duoLiEsp8aip7p7SB3X6XXWJ9K733co6C
dpXotfO0zMnv8l3O9h4pHrrBkmWDBEKbUeuE9Zz7uy6mFAECQQDygylLjzX+2rvm
FYd5ejSaLEeK17AiuT29LNPRHWLu6a0zl923299FCyHLasFgbeuLRCW0LMCs2SKE
Y+cIWMSRAkEA7AnzWjby8j8efjvUwIWh/L5YJyWlSgYKlR0zdgKxxUy9+i1MGRkn
m81NLYza4JLvb8/qjUtvw92Zcppxb7E7wQJAIuQWC+X12c30nLzaOfMIIGpgfKxd
jhFivZX2f66frkn2fmbKIorCy7c3TIH2gn4uFmJenlaV/ghbe/q3oa7L0QJAFP19
ipRAXpKGX6tqbAR2N0emBzUt0btfzYrfPKtYq7b7XfgRQFogT5aeOmLARCBM8qCG
tzHyKnTWZH6ff9M/AQJBAIToUPachXPhDyOpDBcBliRNsowZcw4Yln8CnLqgS9H5
Ya8iBJilFm2UlcXfpUOk9bhBTbgFp+Bv6BZ2Alag7pY=
-----END RSA PRIVATE KEY-----";

if (!$privateKey = openssl_pkey_get_private($private)) die('Loading Private Key failed');
if (!$publicKey = openssl_pkey_get_public($public)) die('Loading Public Key failed');

我还将包括一个小的编码/解码示例:

$encrypted = '';
$decrypted = '';

$plaintext = 'This is just some text to encrypt';

echo '<p>$plaintext = ' . $plaintext . '<p>';

if (!openssl_public_encrypt($plaintext, $encrypted, $publicKey))    die('Failed to encrypt data');

echo '<p>$encrypted = ' . $encrypted . '<p>';

if (!openssl_private_decrypt($encrypted, $decrypted, $privateKey))  die('Failed to decrypt data');

echo '<p>$decrypted = ' . $decrypted . '<p>';

或者以防万一您可以使用 php 生成密钥:

$NEW_KEY = openssl_pkey_new(array(
    'private_key_bits' => 1024,
    'private_key_type' => OPENSSL_KEYTYPE_RSA,
        ));

openssl_pkey_export_to_file($NEW_KEY, 'private.key');

$NEW_KEY_DETAILS = openssl_pkey_get_details($NEW_KEY);
file_put_contents('public.key', $NEW_KEY_DETAILS['key']);

openssl_free_key($NEW_KEY);
于 2012-06-06T21:07:28.213 回答
1

请参考下一个网址。

https://www.php.net/manual/en/function.openssl-pkey-get-private.php

要缩小您的问题范围,请为您的 php 文件和密钥文件使用相同的目录并尝试使用此工作代码。

工作代码

$keyfile="file://".__DIR__.DIRECTORY_SEPARATOR."key.pem"; //absolute path
$key = openssl_pkey_get_private($keyfile);

if ($key === false) {
    var_dump(openssl_error_string());
}else{
    var_dump($key);
}

以下可能是一个问题。

  1. 小路

以下路径样式应该可以工作。

$keyfile="file:///home/john/php/key.pem"; // unix absoulute path
$keyfile="file://C:\\users\\john\\php\\key.pem"; // windows absoulute path
$keyfile="file://".__DIR__.DIRECTORY_SEPARATOR."key.pem"; //absoulute path for unix, windows
$keyfile="file://key.pem"; // relative path, unix, windows, (php,key files in same directory)

$key = openssl_pkey_get_private($keyfile);

如果路径不存在,错误会像

“错误:02001002:系统库:fopen:没有这样的文件或目录”

  1. 网络环境

    检查您的 Web 根和 Web 用户对文件夹和密钥文件的访问权限。

    为了减少问题,请在 php 内置 Web 服务器环境而不是 WAMP 环境上对其进行测试。

>php -S localhost:80
  1. 损坏的密钥文件

保存为包含空格的特定类型。这可能会发生类似下一个错误。

“错误:0906D06C:PEM 例程:PEM_read_bio:没有起始行”

in my case, key file was saved as UTF-8 with BOM(whitespaces)

调试密钥文件 1 - 从变量中读取

这段代码应该可以工作。我从 http://micmap.org/php-by-example/en/function/openssl_pkey_get_private获得了密钥文件

请将 $str 替换为您的。

$str = <<<EOF
-----BEGIN RSA PRIVATE KEY----- 
MIIEogIBAAKCAQEA0llCeBjy18RylTdBih9GMUSZIC3GzeN0vQ9W8E3nwy2jdeUn 
H3GBXWpMo3F43V68zM2Qz5epRNmlLSkY/PJUfJIC8Yc1VEokT52q87hH/XJ5eS8h 
eZnjuSlPAGi8oZ3ImVbruzV7XmlD+QsCSxJW7tBv0dqJ71e1gAAisCXK2m7iyf/u 
l6rT0Zz0ptYH4IZfwc/hQ9JcMg69uM+3bb4oBFsixMmEQwxKZsXk3YmO/YRjRbay 
+6+79bSV/frW+lWhknyGSIJp2CJArYcOdbK1bXx1dRWpbNSExo7dWwuPC0Y7a5AE 
eoZofieQPPBhXlp1hPgLYGat71pDqBjKLvF5GwIDAQABAoIBACPItYsSy3UzYT7L 
OKYTrfBBuD8GKpTqBfkHvAWDa1MD15P92Mr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IFprWA 
P5p/cnvj6aHnqL58XiH0+bE0Lt3J+U6N6JelQQevgBHooMFh4FpDXcVda7xB3rK3 
woqbi8fNhr827H2maxIZPtVG95/mvR4k5z1Jrdnr34ZUmtC6U5Q= 
-----END RSA PRIVATE KEY-----
EOF;

$key = openssl_pkey_get_private($str);
if ($key === false) {
    var_dump(openssl_error_string());
}else{
    var_dump($key);
}

输出

resource(4) 类型(OpenSSL 密钥)

调试密钥文件 2 - 从文件中读取

将您的密钥字符串($str)复制到“key.pem”之类的密钥文件中。

$str = <<<EOF
-----BEGIN RSA PRIVATE KEY----- 
...YOUR KEY STINGS HERE...
-----END RSA PRIVATE KEY-----
EOF;

$str2 = file_get_contents("key.pem");

$len1 = strlen ($str);
$len2 = strlen ($str2);
if($len1 !== $len2) echo "File has been corrupted.";

$key = openssl_pkey_get_private($str2);

if ($key === false) {
    var_dump(openssl_error_string());
}else{
    var_dump($key);
}

于 2019-09-06T00:03:15.917 回答