在Linux中,你可以使用OpenSSL工具將PFX/P12格式的證書轉換為單獨的CRT(證書)、KEY(私鑰)文件以及提取證書鏈
1. 提取私鑰文件(.key)
openssl pkcs12 -in your_certificate.pfx -nocerts -out private.key -nodes
系統會提示你輸入PFX文件的密碼。
2. 提取證書文件(.crt)
openssl pkcs12 -in your_certificate.pfx -clcerts -nokeys -out certificate.crt
3. 提取證書鏈(如果有)
openssl pkcs12 -in your_certificate.pfx -cacerts -nokeys -out chain.crt
4. (可選) 將私鑰轉換為無密碼格式
如果私鑰有密碼保護而你想移除密碼:
openssl rsa -in private.key -out private_unencrypted.key
5. (可選) 驗證提取的文件
驗證私鑰和證書是否匹配:如果兩個命令輸出的MD5值相同,則表示匹配。
openssl x509 -noout -modulus -in certificate.crt | openssl md5
openssl rsa -noout -modulus -in private.key | openssl md5
參數說明:
-in your_certificate.pfx:指定輸入的PFX文件-nocerts:不輸出證書,只輸出私鑰-nokeys:不輸出私鑰,只輸出證書-nodes:不加密私鑰-clcerts:只輸出客戶端證書(不輸出CA證書)
注意事項:
- 確保在安全的環境中操作,因為私鑰是敏感信息
- 如果PFX文件有密碼保護,上述命令會提示你輸入密碼
- 某些情況下,證書鏈可能已經包含在.crt文件中,不需要單獨提取
- 你可以使用
-password pass:yourpassword
參數來避免交互式密碼輸入(不推薦用于生產環境)
完整示例:
# 提取私鑰
openssl pkcs12 -in example.pfx -nocerts -out example.key -nodes# 提取主證書
openssl pkcs12 -in example.pfx -clcerts -nokeys -out example.crt# 提取證書鏈
openssl pkcs12 -in example.pfx -cacerts -nokeys -out chain.crt# 合并證書和證書鏈(某些服務器需要)
cat example.crt chain.crt > fullchain.crt