openssl_pkey_get_public not open public key, “no start line” error

时光总嘲笑我的痴心妄想 提交于 2019-12-09 05:39:22

问题


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)
}

回答1:


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

createKey(2048)); echo $publickey; ?>

More info:

http://phpseclib.sourceforge.net/




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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).



来源:https://stackoverflow.com/questions/3617293/openssl-pkey-get-public-not-open-public-key-no-start-line-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!