p12(或者pfx)文件里一般存放有CA的根證書,用戶證書和用戶的私鑰
假設我們有一個test.p12文件
在安裝了openssl的linux服務器上執行以下命令:
提取用戶證書:
openssl pkcs12 -in test.p12 -clcerts -nokeys -out cert.pem //pem格式
openssl pkcs12 -in test.p12 -clcerts -nokeys -out cert.crt //crt格式
如果需要攜帶秘鑰,則去掉 -nokeys
openssl pkcs12 -in test.p12 -clcerts -out cert.pem //pem格式 openssl pkcs12 -in test.p12 -clcerts -out cert.crt //crt格式
提取私鑰:
openssl pkcs12 -in test.p12 -nocerts -out key.pem
清除秘鑰中的密碼(在把秘鑰部署到某些服務器上時可能需要清除密碼)
openssl rsa -in key.pem -out newkey.pem
在java中pkcs12 和jks的相互轉換:
JKS → P12 keytool -importkeystore -srckeystore keystore.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore keystore.p12
P12 → JKS
keytool -importkeystore -srckeystore keystore.p12 -srcstoretype PKCS12 -deststoretype JKS -destkeystore keystore.jks