【发布时间】:2024-10-04 07:00:01
【问题描述】:
不知道PHP's OpenSSL扩展是否可以用来生成私钥/公钥/证书对?
【问题讨论】:
标签: php openssl public-key-encryption
不知道PHP's OpenSSL扩展是否可以用来生成私钥/公钥/证书对?
【问题讨论】:
标签: php openssl public-key-encryption
我非常感谢 phihag 的回答,但仍在苦苦挣扎。
最终,this 帮助了:
$privateKeyResource = openssl_pkey_new([
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA
]);
// Save the private key to a file. Never share this file with anyone. See https://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file
openssl_pkey_export_to_file($privateKeyResource, '/path/to/myNewPrivateKey.key');
// Generate the public key for the private key
$privateKeyDetailsArray = openssl_pkey_get_details($privateKeyResource);
// Save the public key to another file. Make this file available to anyone (especially anyone who wants to send you encrypted data).
file_put_contents('/path/to/myNewPublicKey.key', $privateKeyDetailsArray['key']);
// Free the key from memory.
openssl_free_key($privateKeyResource);
查看文档:
【讨论】:
当然,使用openssl_pkey_new:
$privateKey = openssl_pkey_new(array('private_key_bits' => 2048));
$details = openssl_pkey_get_details($privateKey);
$publicKey = $details['key'];
您可以使用openssl_pkey_export 或openssl_pkey_export_to_file 导出密钥。
【讨论】: