为什么在 Linux 或 Windows 下私钥字符串不同?

2023-10-15php开发问题
3

本文介绍了为什么在 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 下私钥字符串不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17