php openssl_pkey_get_public 未打开公钥,“无起始行”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3617293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:26:18  来源:igfitidea点击:

openssl_pkey_get_public not open public key, "no start line" error

phpopensslrsapublic-key

提问by slamer

When generating public key and then reading it with function openssl_pkey_get_public- $publicKeyResource= bool(false) and message: error:0906D06C:PEM routines:PEM_read_bio:no start line

当生成公钥然后使用函数openssl_pkey_get_public- $publicKeyResource= bool(false) 和消息读取它时: 错误:0906D06C:PEM 例程:PEM_read_bio:no start line

$privateKey = openssl_pkey_new(array('private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA));

$keyDetails = openssl_pkey_get_details($privateKey);

$publicKeyResource = openssl_pkey_get_public($keyDetails['key']);

What is wrong?

怎么了?

P.S.

聚苯乙烯

privateKey = 

array(3) {
  ["bits"]=>int(2048)

  ["key"]=>
  string(451) "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApo5lpSuSQmAOXfqAmexj
IzjdGnd1X1gCKj5ko2DHgcR4XBlj1hbFNs1pzXx+R/UvLXTeF7dNQ+9AgXjEeRa6
71VbNxrUgvb/PHjEANwce7xBsnbu+dcSazyNHzx4ahWyEF4f3HyaJkGrT/Dgzcut
DO+yFAH9u8Hx26cj/8kyrtIHxazemnD+IDHRa3zOjKDmTfoDRKtOMTPVgFAsYBXn
tKcLyamCSBgpwfQwKfUUcYhfY1xD9UMhVXabSSiNQOiTMuOIZUHueO8UCp/tdK6a
LprUDBQ/tVmiV7ZMeZYMjh6XnK7higJ3WZp8RmD4PPeKbtG6j2AuGpbF/ddzD62T
XwIDAQAB
-----END PUBLIC KEY-----
"

  ["type"]=>
  int(0)
}

采纳答案by Archimedix

You might have an easier time with phpseclib's Crypt_RSA. eg.

您可能会更轻松地使用 phpseclib 的 Crypt_RSA。例如。

创建密钥(2048));回声 $publickey; ?>

More info:

更多信息:

http://phpseclib.sourceforge.net/

http://phpseclib.sourceforge.net/

回答by Archimedix

Same answer as I gave in PHP + OpenSSL : error returned, but correct result: This is apparently caused by openssl_pkey_get_public()which wants a certificate containing your public key rather than the public key by itself – it seems to load the public key but still causes this error. See details there.

与我在PHP + OpenSSL 中给出的答案相同:错误返回,但结果正确:这显然是由openssl_pkey_get_public()引起的,它想要一个包含您的公钥而不是公钥的证书 - 它似乎加载了公钥但仍然导致此错误。在那里查看详细信息。

回答by Sidora Gleb

Most likely PHP's openSSL rejects key in PEM form. Try RSA form instead

很可能 PHP 的 openSSL 拒绝 PEM 形式的密钥。尝试使用 RSA 形式

回答by caf

I suspect that this must be a bug in some version of either PHP or OpenSSL, because your posted code works fine for me (with PHP 5.2.6 and OpenSSL 0.9.8g).

我怀疑这一定是某些版本的 PHP 或 OpenSSL 中的错误,因为您发布的代码对我来说很好用(使用 PHP 5.2.6 和 OpenSSL 0.9.8g)。

回答by shamittomar

Reason:

原因:

This error is usually caused by one corrupt character at the beginning of the .crt file. So, the chances are that you have an extra space, an extra character, an extra line, etc. in either the SSL Certificate file (.crt) or the SSL key.

此错误通常是由 .crt 文件开头的一个损坏字符引起的。因此,您可能在 SSL 证书文件 (.crt) 或 SSL 密钥中有额外的空格、额外的字符、额外的行等。