lpilp / phpsm2sm3sm4

php版本,支持国密SM2的签名算法,非对称加解密,SM3的hash, SM4的对称加解密
320 stars 74 forks source link

再问个问题我的私钥是pfx后缀的,跟代码里的公私钥不一样,要怎么处理? #59

Closed weishuiliang closed 1 year ago

lpilp commented 1 year ago

pfx的密钥应该是加密过的,无法解成明文,目前本项目暂不支持,请解成明文后再使用

weishuiliang commented 1 year ago

我再mac下用下面的命令转: 导出私钥: openssl pkcs12 -in filename.pfx -nocerts -out key.pem 导出证书: openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem

cert.pem: Bag Attributes friendlyName: 2522862421524611105 localKeyID: 54 69 6D 65 20 31 36 38 39 32 33 34 36 37 31 36 33 32 subject=/C=CN/O=CFCA SM2 OCA32/OU=helipay/OU=Scene/CN=051@\xE5\x98\x89\xE8\xAF\xBA@ZC1807621026@1 issuer=/C=CN/O=China Financial Certification Authority/CN=CFCA ACS SM2 OCA32 -----BEGIN CERTIFICATE----- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ....
-----END CERTIFICATE-----

key.pem: Bag Attributes friendlyName: 2522862421524611105 localKeyID: 54 69 6D 65 20 31 36 38 39 32 33 34 36 37 31 36 33 32

感觉也不对啊,我看私钥都是很短的

lpilp commented 1 year ago

这个是一个标准的公钥证书文件格式 cert.pem中:

-----BEGIN CERTIFICATE-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ....
-----END CERTIFICATE-----

用asn1可解出明文的公钥了,04a54*****11b2 key.pem中应该也有个 ---- BEGIn private ---- 的吧,这个可以解出明文的私钥, 你自行解开就行了,不用再贴出来

weishuiliang commented 1 year ago

你怎么解成明文的? 用openssl吗? 怎么弄?

lpilp commented 1 year ago

项目有一个asn1解析文件测试文件,使用参考 test/tasn1.php 将

-----BEGIN CERTIFICATE-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ....
-----END CERTIFICATE-----

写到文件里测试 ,可以解出一个数组,你也可以找一个 asn1 decode的其他项目解开, asn1就是将一个数组给序列化了,

fengyun21 commented 5 months ago

$res=openssl_pkcs12_read(file_get_contents($pfxPath), $certs, $password);