将OpenSSL公钥导出到文件

时间:2015-03-04 23:28:32

标签: php openssl php-openssl

如何使用PHP仅将公钥导出到文件(.pem)? 当然可以导出整个证书和私钥,但我找不到导出公钥的解决方案。

该文件应该看起来像

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArDjwWcPjZRQpiMRsG1fG
kudSYEHnSR57dKUoK+qIc++YZSCWG/q460LrNtQ/EBx6ZdJJnhBk4EwY0snb7TEr
pH5efU2BuCygO3f78DozMxNvvHFFfZ1uw4hobBMSKLKtVcJKmh7r7dvgXtKWdqJf
sUmr2V3LDXTF3r6G5NEorGC4crcSByAvyPatUKRR5t6kAuU8pnWrHnDo+GoSjHy2
vvaUuds6FxHMieFXKrnN8dk6VPpV2PEwzSAk9Ofbq3oZxkIhVGTHjgFPF9tIx3Vu
yo+S4KCP6sxmEt1/p4B5VsCsRU4STbA5jC8qK9vJ1YhMD6tK5FWreaDiWL3wBjf5
xwIDAQAB
-----END PUBLIC KEY-----

1 个答案:

答案 0 :(得分:2)

这就是解决方案。感谢https://www.virendrachandak.com/techtalk/encryption-using-php-openssl/

// Generate the public key for the private key
$key = openssl_pkey_get_details($privateKey); // $privateKey is created with openssl_pkey_new()

// Save the public key in public.pem file
file_put_contents('public.pem', $key['key']);