Loading...
2026-01-16 12:48
81

小編目前的轉換環境是:

Ubuntu 22.04

OpenSSL 3.0.2

1. 建立暫時的舊版憑證副本

請將 CERTIFICATE_FILE_NAME 替換為實際的 PKCS12 憑證檔案名稱。

 
cp CERTIFICATE_FILE_NAME.p12 temp_certificate.p12

2. 轉換為 PEM 格式

temp_certificate.p12 從 PKCS12 格式轉換為 PEM 格式的 temp_certificate.pem 檔案,且在輸出檔中不加密私鑰
請將 CERTIFICATE_PASSWORD 替換為實際的憑證密碼。

 
openssl pkcs12 -legacy -in temp_certificate.p12 -out temp_certificate.pem -nodes -passin pass:CERTIFICATE_PASSWORD

3. 擷取私鑰

從 PKCS12 格式的 temp_certificate.p12 檔案中擷取私鑰,並儲存為 temp_certificate.key 檔案。
輸出的私鑰檔案將會是加密的

 
openssl pkcs12 -legacy -in temp_certificate.p12 -nocerts -out temp_certificate.key -passin pass:CERTIFICATE_PASSWORD -passout pass:'CERTIFICATE_PASSWORD'

4. 建立新的憑證

使用 temp_certificate.key 中的私鑰與 temp_certificate.pem 中的憑證,產生一個新的 PKCS12 格式憑證檔 new_certificate.p12

 
openssl pkcs12 -export -out new_certificate.p12 -inkey temp_certificate.key -in temp_certificate.pem -passin pass:CERTIFICATE_PASSWORD -passout pass:CERTIFICATE_PASSWORD

5. 刪除暫存檔案

刪除所有建立過程中產生的暫存檔案。

 
rm -rf temp_certificate.p12 temp_certificate.pem temp_certificate.key
新版的 new_certificate.p12 可以直接放到專案中了!