openssl_verify 和“错误:0906D06C:PEM 例程:PEM_read_bio:无起始行”
我正在尝试在 PHP 中使用 OpenSSL 函数进行 RSA 签名/验证。 当我尝试使用公钥执行 openssl_verify
时,收到此错误:错误:0906D06C:PEM 例程:PEM_read_bio:无起始行
< /em>,但函数本身可以正常工作(如果消息被修改则返回 0,如果完好则返回 1)。 openssl_sign
工作正常。
我该如何修复它?
目前,我使用 openssl 生成的公钥:
define("SC_MSG_PUBLIC", <<<EOD
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALjPcOckMHDVLiUstcRwwx8kF5PzsiEs
rskyndWisbXMLU9BHomXwL7Qg2L91jE+sNSobkzBDF41CbwDiNlofZUCAwEAAQ==
-----END PUBLIC KEY-----
EOD
);
有什么想法为什么会触发此错误,但一切正常?
尝试从私有生成公钥并使用它,但它似乎完全相同,相同的错误消息:-S
$pkey = openssl_pkey_get_private(SC_MSG_PRIVATE);
$keyDetails = openssl_pkey_get_details($pkey);
file_put_contents('c:\publickey', $keyDetails['key']);
另外,我尝试安装所有内容的较新版本(PHP 5.3.1、OpenSSL 1.0.0a ) - 相同的结果。而且,我在窗户上。
I am trying to use OpenSSL function for RSA sign/verify in PHP.
When I try to do openssl_verify
using my public key, I am getting this error: error:0906D06C:PEM routines:PEM_read_bio:no start line
, but the function itself works correctly (returns 0 if messages was modified, and 1 if intact). openssl_sign
works fine.
How can I fix it?
Currently, I use public key generated by openssl:
define("SC_MSG_PUBLIC", <<<EOD
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALjPcOckMHDVLiUstcRwwx8kF5PzsiEs
rskyndWisbXMLU9BHomXwL7Qg2L91jE+sNSobkzBDF41CbwDiNlofZUCAwEAAQ==
-----END PUBLIC KEY-----
EOD
);
Any ideas why this error triggers, but things works fine?
Tried to generate public key out of private, and use it, but it appeared to be exactly the same, same error message :-S
$pkey = openssl_pkey_get_private(SC_MSG_PRIVATE);
$keyDetails = openssl_pkey_get_details($pkey);
file_put_contents('c:\publickey', $keyDetails['key']);
Also, I've tried to install newer versions of everything (PHP 5.3.1, OpenSSL 1.0.0a) - same result. And, I am on windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
原因:
此错误通常是由 .crt 文件开头的一个损坏字符引起的。因此,SSL 证书文件 (.crt) 或 SSL 密钥文件 (.key) 中可能存在额外的空格、额外的字符、额外的行等。
可能的解决方案:
。
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 file (.key).
Possible Solution(s):
.
您可能会更轻松地使用 phpseclib 进行签名创建/验证:
http://phpseclib。 sourceforge.net/documentation/misc_crypt.html#misc_crypt_rsa_examples
You might have an easier time using phpseclib for signature creation / verification:
http://phpseclib.sourceforge.net/documentation/misc_crypt.html#misc_crypt_rsa_examples
其他人都有一个 errno,成功操作后会自动重置为零,而 OpenSSL 有一个“错误堆栈”,您需要手动清空它。请参阅函数 openssl_error_string ,它是 根据 ERR_get_error。您看到的错误消息很可能与您的代码无关;尝试在代码之前添加此内容:
以及在每行之间:
Where everyone else has an errno that is reset to zero automatically by successful operations, OpenSSL has an "error stack", that you need to empty manually. See function openssl_error_string which is implemented in terms of ERR_get_error. Chances are that the error message that you are seeing has nothing to do with your code; try adding this before your code:
and in between each line:
您是否尝试使用包含您的公钥的(可能是自签名的)证书而不是纯公钥来调用 openssl_verify() ?
据我所知,一些 PHP OpenSSL 函数无法正确支持裸公钥,尽管尽管出现错误但它确实正确验证似乎很奇怪。
在 Linux/UNIX shell(例如 bash)中将公钥转换为简单证书的示例(请参阅 OpenSSL 文档或一些教程以了解更多信息):
这还将创建您之后可能想要删除的临时文件,即 [请求-TMP-文件]</em> 和[随机-TMP-文件]</em>。
PHP 示例代码可以在 http://de.php 找到。 net/manual/en/function.openssl-csr-new.php。
Have you tried to call openssl_verify() with a (maybe self-signed) certificate containing your public key instead of a pure public key ?
As far as I know, some PHP OpenSSL functions do not properly support naked public keys although it seems strange that it does verify correctly in spite of the error.
Example for converting a public key to a simple certificate in a Linux/UNIX shell such as bash (refer to the OpenSSL documentation or some tutorials for more):
This will also create temporary files you might want to delete afterwards, namely [REQUEST-TMP-FILE] and [RANDOM-TMP-FILE].
PHP sample code can be found at http://de.php.net/manual/en/function.openssl-csr-new.php.