openssl_pkey_get_public 未公开公钥,“无起始行”错误
生成公钥然后使用函数 openssl_pkey_get_public - $publicKeyResource = bool(false) 和消息读取它时: 错误:0906D06C:PEM例程:PEM_read_bio:无起始行
$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']);
出了什么问题?
聚苯乙烯
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)
}
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
$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)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
与我在 PHP + OpenSSL 中给出的答案相同:返回错误,但正确的结果:
这显然是由 openssl_pkey_get_public() 引起的,它想要一个包含您的公钥的证书,而不是公钥本身 - 它似乎加载了公钥,但仍然导致此错误。 查看详细信息。
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.
使用 phpseclib 的 Crypt_RSA 可能会更轻松。例如。
createKey(2048));
echo $publickey;
?>
更多信息:
http://phpseclib.sourceforge.net/
You might have an easier time with phpseclib's Crypt_RSA. eg.
createKey(2048));
echo $publickey;
?>
More info:
http://phpseclib.sourceforge.net/
PHP 的 openSSL 很可能拒绝 PEM 形式的密钥。尝试使用 RSA 形式
Most likely PHP's openSSL rejects key in PEM form. Try RSA form instead
原因:
此错误通常是由 .crt 文件开头的一个损坏字符引起的。因此,SSL 证书文件 (.crt) 或 SSL 密钥中可能存在额外的空格、额外的字符、额外的行等。
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.
我怀疑这一定是 PHP 或 OpenSSL 某些版本中的错误,因为您发布的代码对我来说工作正常(使用 PHP 5.2.6 和 OpenSSL 0.9.8g)。
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).