1. 程式人生 > 其它 >使用openssl進行證書格式轉換

使用openssl進行證書格式轉換

各類證書由於儲存的內容不同(如是否包含公鑰/私鑰是否加密儲存/單一證書或多證書等)、採用編 碼不同(DER/BASE64)、標準不同(如PEM/PKCS),所以儘管X.509標準規定了證書內容規範,但證書檔案還是五花八門。好在 openssl對這些不同的標準都有著不錯的支援,可以用來進行不同格式證書的轉換。

大體來說,證書轉換要作的工作有這麼幾種


編碼轉換:DER<-->BASE64
不同證書標準的轉換:PKCS系列<-->PEM/CER
私鑰的增/減/提取/轉換
...

PEM--DER/CER(BASE64--DER編碼的轉換)

openssl x509 -outform der -in certificate.pem -out certificate.der

PEM--P7B(PEM--PKCS#7)

openssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CACert.cer

PEM--PFX(PEM--PKCS#12)

openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt

PEM--p12(PEM--PKCS#12)

openssl pkcs12 -export -out Cert.p12 -in Cert.pem -inkey key.pem

CER/DER--PEM(編碼DER--BASE64)

openssl x509 -inform der -in certificate.cer -out certificate.pem

P7B--PEM(PKCS#7--PEM)

openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer

P7B--PFX(PKCS#7--PKCS#12)

openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer
openssl pkcs12 -export -in certificate.cer -inkey privateKey.key -out certificate.pfx -certfile CACert.cer

PFX/p12--PEM(PKCS#12--PEM)

openssl pkcs12 -in certificate.pfx -out certificate.cer
如無需加密pem中私鑰,可以新增選項-nodes;
如無需匯出私鑰,可以新增選項-nokeys;

PEM BASE64--X.509文字格式

openssl x509 -in Key.pem -text -out Cert.pem

PFX檔案中提取私鑰(.key)

openssl pkcs12 -in mycert.pfx -nocerts -nodes -out mycert.key

PEM--SPC

openssl crl2pkcs7 -nocrl -certfile venus.pem -outform DER -out venus.spc

PEM--PVK(openssl 1.x開始支援)

openssl rsa -in mycert.pem -outform PVK -pvk-strong -out mypvk.pvk

PEM--PVK(對於openssl 1.x之前的版本,可以下載pvk轉換器後通過以下命令完成)

pvk -in ca.key -out ca.pvk -nocrypt -topvk

PVK格式更多參考:http://www.drh-consultancy.demon.co.uk/pvk.html

openssl更多選項及功能,請參考openssl手冊

轉自https://www.cnblogs.com/zhengah/p/5010613.html