是否可以将.pfx(个人信息交换)文件转换为.cer(安全证书)文件?除非我弄错了,.cer不是以某种方式嵌入在.pfx中吗?如果可能的话,我想要一种提取它的方法。
是否可以将.pfx(个人信息交换)文件转换为.cer(安全证书)文件?除非我弄错了,.cer不是以某种方式嵌入在.pfx中吗?如果可能的话,我想要一种提取它的方法。
PFX文件是PKCS#12个人信息交换语法标准的捆绑包。它们可以包括任意数量的私钥与相应的X.509证书以及证书颁发机构链(一组证书)。
如果您想提取客户端证书,您可以使用OpenSSL的PKCS12工具。
openssl pkcs12 -in input.pfx -out mycerts.crt -nokeys -clcerts
以上命令将以PEM格式输出证书。 ".crt"文件扩展名在macOS和Windows中均可处理。
您在问题中提到了".cer"扩展名,它通常用于DER编码的文件。二进制编码。首先尝试使用“.crt”文件,如果不被接受,则很容易从PEM转换为DER:
openssl x509 -inform pem -in mycerts.crt -outform der -out mycerts.cer
我认为简单的方法是使用Windows管理控制台中的证书管理器导入和导出,以此来处理相关技术。
如果你正在使用PowerShell,可以使用以下内容生成DER编码(二进制)证书文件OutputCert.der,假设有一个pfx文件InputBundle.pfx:
$pwd = ConvertTo-SecureString -String "YourPFXPassword" -Force -AsPlainText
Import-PfxCertificate -FilePath "C:\Path\To\InputBundle.pfx" -CertStoreLocation cert:\CurrentUser\My -Password $pwd
$cert = Get-ChildItem -Path "cert:\CurrentUser\My" | Where-Object {$_.Subject -eq "YourCertificateSubjectName"}
$binaryCert = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)
[io.file]::WriteAllBytes("C:\Path\To\OutputCert.der", $binaryCert)
Get-PfxCertificate -FilePath InputBundle.pfx |
Export-Certificate -FilePath OutputCert.der -Type CERT
为了清晰起见添加了换行符,但当然您可以将所有内容放在单行中。
如果您需要以ASCII/Base64编码PEM格式获取证书,则可以采取其他措施进行操作,具体文档请参见此处:https://superuser.com/questions/351548/windows-integrated-utility-to-convert-der-to-pem
如果您需要导出不同于DER编码的格式,则可以更改Export-Certificate的-Type
参数,使用.NET支持的类型,如在help Export-Certificate -Detailed
中所示:
-Type <CertType>
Specifies the type of output file for the certificate export as follows.
-- SST: A Microsoft serialized certificate store (.sst) file format which can contain one or more certificates. This is the default value for multiple certificates.
-- CERT: A .cer file format which contains a single DER-encoded certificate. This is the default value for one certificate.
-- P7B: A PKCS#7 file format which can contain one or more certificates.
我想添加一个最简单的方法。
右键单击pfx文件,选择“安装”,按照向导操作并将其添加到存储区(我添加到了个人存储区)。
在开始菜单中输入certmgr.msc,进入CertManager程序。
找到您的pfx证书(顶部的选项卡是各种存储区),单击“导出”按钮并按照向导操作(有一个导出为.CER的选项)
本质上它与安德鲁的答案相同,但它避免了使用Windows管理控制台(直接进入导入/导出过程)。
[+1]
- kapitan# Extracting ca-certs..."
openssl pkcs12 -in ${filename}.pfx -nodes -nokeys -cacerts -out ${filename}-ca.crt
# Extracting key file..."
openssl pkcs12 -in ${filename}.pfx -nocerts -out ${filename}.key
# Extracting crt..."
openssl pkcs12 -in ${filename}.pfx -clcerts -nokeys -out ${filename}.crt
# combine ca-certs and cert files
cat ${filename}.crt ${filename}-ca.crt > ${filename}-full.crt
# Removing passphrase from keyfile"
openssl rsa -in ${filename}.key -out ${filename}.key
将.pfx文件转换为.pem格式
有时您需要将.pfx文件转换为.pem格式。运行以下命令以将其转换为PEM格式。
openssl rsa -in [keyfile-encrypted.key] -outform PEM -out [keyfile-encrypted-pem.key]
来源:https://www.ibm.com/docs/en/arl/9.7?topic=certification-extracting-certificate-keys-from-pfx-file
这可能与提问者的问题无关,但我已尝试使用所有不同的标志和openssl语句,同时尝试使用PHP \SoapClient(...)
进行连接,在经过3天的尝试之后,我终于找到了适合我的解决方案。
$ cd path/to/certificate/
$ openssl pkcs12 -in personal_certificate.pfx -out public_key.pem -clcerts
首先,您需要输入YOUR_CERT_PASSWORD
一次,然后再输入DIFFERENT_PASSWORD!
两次。后者可能对所有具有访问代码权限的人都可用。
$wsdlUrl = "https://example.com/service.svc?singlewsdl";
$publicKey = "rel/path/to/certificate/public_key.pem";
$password = "DIFFERENT_PASSWORD!";
$params = [
'local_cert' => $publicKey,
'passphrase' => $password,
'trace' => 1,
'exceptions' => 0
];
$soapClient = new \SoapClient($wsdlUrl, $params);
var_dump($soapClient->__getFunctions());
openssl rsa -in f.pem -inform PEM -out f.der -outform DER