該文章轉載自 OHTTPS.COM - 免費申請HTTPS通配符證書、自動更新、自動部署 在部署HTTPS證書時,不同的伺服器我們需要用到不同格式的證書檔案,常見的證書檔案格式有以下幾種:
- PEM
- 适用于Apache、Nginx、Candy Server等Web伺服器
- 常見的檔案字尾為.pem、.crt、.cer、.key
- 可以存放證書或私鑰,或者兩者都包含
- .key字尾一般隻用于證書私鑰檔案
- PFX
- 适用于IIS等Web伺服器
- 常見的檔案字尾為.pfx、.p12
- 同時包含證書和私鑰,且一般有密碼保護
- JKS
- 适用于Tomcat、Weblogic、JBoss、Jetty等Web伺服器
- 常見的檔案字尾為.jks
Let's Encrypt頒發的HTTPS證書一般包括以下幾個檔案:
- cert.key(PEM格式):私鑰檔案
- cert.cer(PEM格式):證書檔案
- fullchain.cer(PEM格式):包含證書和中間證書
下面我們介紹如何使用cert.key、cert.cer、fullchain.cer生成cert.pfx、cert.jks,以及它們之間如何互相轉換
-
PEM ===> PFX
- 工具: openssl
- 指令:使用cert.key和fullchain.cer檔案生成cert.pfx
openssl pkcs12 -export -out cert.pfx -inkey cert.key -in fullchain.cer
-
PFX ===> JKS
- 工具:keytool
- 指令:使用cert.pfx生成cert.jks
keytool -importkeystore -srckeystore cert.pfx -destkeystore cert.jks -srcstoretype PKCS12 -deststoretype JKS
-
PEM ===> JKS
- 需要使用上面的兩個方法,先将PEM檔案轉換為PFX檔案,然後再将PFX檔案轉換為JKS檔案
-
PFX ===> PEM
openssl pkcs12 -in cert.pfx -nodes -out temp.cer
- 指令2:使用臨時檔案temp.cer檔案生成私鑰檔案cert.key
openssl rsa -in temp.cer -out cert.key
- 指令3:使用臨時檔案temp.cer檔案生成證書檔案cert.cer
openssl x509 -in temp.cer -out cert.cer
- 指令4:使用cert.pfx生成中間證書檔案chain.cer,合并cert.cer、空白行、chain.cer即可得到fullchain.cer
openssl pkcs12 -in cert.pfx -cacerts -nokeys -chain | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > chain.cer echo '\n' > emptyline.cer cat cert.cer emptyline.cer chain.cer> fullchain.cer
-
JKS ===> PFX
- 指令:使用cert.jks生成cert.pfx
keytool -importkeystore -srckeystore cert.jks -destkeystore cert.pfx -srcstoretype JKS -deststoretype PKCS12
除了以上這些方法之外,還可以使用線上工具進行證書格式轉換,點選
證書格式轉換工具進行線上格式轉換。