将.pfx转换为.cer

时间:2008-12-31 15:10:51

标签: security certificate pfx

是否可以将.pfx(个人信息交换)文件转换为.cer(安全证书)文件?除非我弄错了,不是一个.cer以某种方式嵌入.pfx中?如果可能的话,我想要一些方法来提取它。

6 个答案:

答案 0 :(得分:171)

PFX文件是PKCS#12 Personal Information Exchange Syntax Standard个捆绑包。它们可以包含任意数量的私钥,附带X.509证书和证书颁发机构链(设置证书)。

如果要提取客户端证书,可以使用OpenSSL's PKCS12 tool

openssl pkcs12 -in input.pfx -out mycerts.crt -nokeys -clcerts

上述命令将以PEM格式输出证书。 “.crt”文件扩展名由macOS和Window处理。

您在常规用于DER编码文件的问题中提到“.cer”扩展名。二进制编码。首先尝试“.crt”文件,如果不接受,可以轻松地从PEM转换为DER:

openssl x509 -inform pem -in mycerts.crt -outform der -out mycerts.cer

答案 1 :(得分:77)

我认为使用Windows管理控制台中的证书管理器导入它然后导出它的简单方法。

答案 2 :(得分:32)

如果您在PowerShell中工作,则可以使用以下内容,在给定pfx文件 InputBundle.pfx 的情况下,生成DER编码(二进制)证书文件 OutputCert。 DER

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.

答案 3 :(得分:21)

我想添加一个我认为最简单的方法。

  1. 只需右键单击pfx文件,单击“安装”按照向导,然后将其添加到商店(我添加到个人商店)。

  2. 在开始菜单中键入certmgr.msc并转到CertManager程序。

  3. 找到您的pfx证书(顶部的标签是各个商店),点击导出按钮并按照向导(有一个选项导出为.CER)

  4. 基本上它与安德鲁的答案完全相同,但它避免使用Windows管理控制台(直接导入/导出)。

答案 4 :(得分:0)

openssl rsa -in f.pem -inform PEM -out f.der -outform DER

答案 5 :(得分:0)

可能与 OP 的 Q 无关,但我尝试使用 all openssl 语句和 all 不同的标志,同时尝试与 PHP \SoapClient(...) 和3 天后,我终于找到了适合我的解决方案。

GitBash

$ cd path/to/certificate/
$ openssl pkcs12 -in personal_certificate.pfx -out public_key.pem -clcerts

首先您必须输入一次 YOUR_CERT_PASSWORD,然后输入两次 DIFFERENT_PASSWORD!。后者可能可供所有有权访问代码的人使用。

PHP

<?php

$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());