为什么在 Linux 或 Windows 下私钥字符串不同? [英] Why different private key strings under Linux or Windows?

查看:24
本文介绍了为什么在 Linux 或 Windows 下私钥字符串不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下 PHP 代码(和相同的配置参数)创建私钥字符串时,它们包含在不同的字符串之间:

When I'm creating private key strings with the following PHP code (and same config-parameter), they are enclosed between different strings:

$configs = array('config' => 'OpenSSL.cnf',
                 'digest_alg' => 'sha1',
                 'x509_extensions' => 'v3_ca',
                 'req_extensions' => 'v3_req',
                 'private_key_bits' => 2048,
                 'private_key_type' => OPENSSL_KEYTYPE_RSA,
                 'encrypt_key' => false,
                 'encrypt_key_cipher' => OPENSSL_CIPHER_3DES);

$privateKeyResourceId = openssl_pkey_new($this->configs);                       
openssl_pkey_export($privateKeyResourceId, $privateKeyString);

在 Linux 上 $privateKeyString 看起来像这样:

On Linux the $privateKeyString looks like this:

-----开始私钥-----NBgkqhkiG9w0BAQE....ASDFasjkfa-----结束私钥-----

-----BEGIN PRIVATE KEY-----NBgkqhkiG9w0BAQE....ASDFasjkfa-----END PRIVATE KEY-----

在 Windows 上,$privateKeyString 如下所示:

On Windows the $privateKeyString looks like this:

-----开始 RSA 私钥-----NBgkqhkiG9E....ASDFasjkfa-----结束 RSA 私钥-----

-----BEGIN RSA PRIVATE KEY-----NBgkqhkiG9E....ASDFasjkfa-----END RSA PRIVATE KEY-----

当我将 Windows 私钥字符串复制到 Linux 时,它会一直工作,直到我从开头/结尾删除RSA"(反之亦然).这是为什么?

When I copy the Windows private key string to Linux it works until I remove the 'RSA' from the start/end (same behavior vice versa). Why is this?

推荐答案

根据 用户注意 php.net 这是一个已知问题:

According to a user note php.net this is a known issue:

请注意,旧版本的 PHP/OpenSSL 导出带有 '-----BEGIN RSA PRIVATE KEY-----' PEM 标记的 RSA 私钥,其中仅包含 privateKey 字段,因此省略了版本和privateKeyAlgorithm 字段.

Please take note that older versions of PHP/OpenSSL exports the RSA private key with '-----BEGIN RSA PRIVATE KEY-----' PEM tag, which includes just the privateKey field, thus omitting the version and privateKeyAlgorithm fields.

这样做的效果是,如果您将其转换为 DER,并且然后回到 PEM,但使用 '-----BEGIN PRIVATE KEY-----' PEM 标签,openssl_pkey_get_privatekey() 函数将失败!Senthryl 的代码可用于在 PEM 编码数据前加上版本和再次使用 privateKeyAlgorithm 字段.

The effect of that would be that if you're converting it to DER, and then back to PEM, but using '-----BEGIN PRIVATE KEY-----' PEM tag, that the openssl_pkey_get_privatekey() function will fail!Senthryl's code can be used to prefix the PEM encoded data with the version and privateKeyAlgorithm fields again.

较新的 PHP/OpenSSL 版本导出 RSA 私钥'-----BEGIN PRIVATE KEY-----' PEM 标签,包括版本和privateKeyAlgorithm 字段.

The newer PHP/OpenSSL versions exports the RSA private key with '-----BEGIN PRIVATE KEY-----' PEM tag, which includes the version and privateKeyAlgorithm fields.

我注意到我的两台服务器之间存在这些差异:

I noticed these differences between my two servers:

基于 Fedora Core 12 x64 的 PHP 版本 5.3.3(OpenSSL 1.0.0a-fips,2010 年 6 月 1 日)

PHP Version 5.3.3 (OpenSSL 1.0.0a-fips 1 Jun 2010) on Fedora Core 12 x64

基于 Fedora Core 10 x64 的 PHP 版本 5.2.9(OpenSSL 0.9.8g 2007 年 10 月 19 日)

PHP Version 5.2.9 (OpenSSL 0.9.8g 19 Oct 2007) on Fedora Core 10 x64

这篇关于为什么在 Linux 或 Windows 下私钥字符串不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆