1. 程式人生 > >證書之間的轉換(crt pem key)

證書之間的轉換(crt pem key)

在ssh協議中,採用一種非對稱加密的方式保障通訊安全,即a,b通過tcp建立連線後,b生成一對公私鑰並將公鑰傳送給a,a再將金鑰用b的公鑰傳送給a,後續通訊便使用金鑰加密。
在https協議中,有證書(ca:包含公鑰及其他一些資訊)這種存在。即在http的基礎上加入ssl層,使資料在傳輸過程中加密,證書就是這個過程的一個認證官。
常見公鑰字尾:pem crt key
常見私鑰字尾:pfx p12 pem key

在這裡插入圖片描述
而在api專案中,我們通常是直接利用公私鑰要進行資料驗證的。
如果直接得到了一個證書,需要對證書進行轉換。

格式說明

PKCS 全稱是 Public-Key Cryptography Standards ,是由 RSA 實驗室與其它安全系統開發商為促進公鑰密碼的發展而制訂的一系列標準,PKCS 目前共釋出過 15 個標準。 常用的有:
PKCS#7 Cryptographic Message Syntax Standard
PKCS#10 Certification Request Standard
PKCS#12 Personal Information Exchange Syntax Standard
X.509是常見通用的證書格式。所有的證書都符合為Public Key Infrastructure (PKI) 制定的 ITU-T X509 國際標準。
PKCS#7 常用的字尾是: .P7B .P7C .SPC
PKCS#12 常用的字尾有: .P12 .PFX
X.509 DER 編碼(ASCII)的字尾是: .DER .CER .CRT
X.509 PAM 編碼(Base64)的字尾是: .PEM .CER .CRT
.cer/.crt是用於存放證書,它是2進位制形式存放的,不含私鑰。
.pem跟crt/cer的區別是它以Ascii來表示。
pfx/p12用於存放個人證書/私鑰,他通常包含保護密碼,2進位制方式
p10是證書請求
p7r是CA對證書請求的回覆,只用於匯入
p7b以樹狀展示證書鏈(certificate chain),同時也支援單個證書,不含私鑰。
用openssl建立CA證書的RSA金鑰(PEM格式):


openssl genrsa -des3 -out ca.key 1024
用openssl建立CA證書(PEM格式,假如有效期為一年):
openssl req -new -x509 -days 365 -key ca.key -out ca.crt -config openssl.cnf
x509轉換為pfx
openssl pkcs12 -export -out server.pfx -inkey server.key -in server.crt
PEM格式的ca.key轉換為Microsoft可以識別的pvk格式
pvk -in ca.key -out ca.pvk -nocrypt -topvk

PKCS#12 到 PEM 的轉換
openssl pkcs12 -nocerts -nodes -in cert.p12 -out private.pem 驗證 openssl pkcs12 -clcerts -nokeys -in cert.p12 -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

用 -outform -inform 指定 DER 還是 PAM 格式。例如:
openssl x509 -in Cert.pem -inform PEM -out cert.der -outform DER
PEM 到 PKCS#12 的轉換
openssl pkcs12 -export -in Cert.pem -out Cert.p12 -inkey key.pem
IIS 證書
cd c:\openssl set OPENSSL_CONF=openssl.cnf openssl pkcs12 -export -out server.pfx -inkey server.key -in server.crt
server.key和server.crt檔案是Apache的證書檔案,生成的server.pfx用於匯入IIS
Convert PFX Certificate to PEM Format for SOAP
$ openssl pkcs12 -in test.pfx -out client.pem Enter Import Password: MAC verified OK Enter PEM pass phrase: Verifying - Enter PEM pass phrase:
DER檔案(.crt .cer .der)轉為PEM格式檔案
轉換DER檔案(一般字尾名是.crt .cer .der的檔案)到PEM檔案 openssl x509 -inform der -in certificate.cer -out certificate.pem 轉換PEM檔案到DER檔案 openssl x509 -outform der -in certificate.pem -out certificate.der

cer檔案中匯出公鑰

cer轉pem
openssl x509 -inform der -in ***pds.cer -out certificate.pem
在這裡插入圖片描述
在這裡插入圖片描述

p12 檔案匯出公鑰私鑰

生成key檔案
openssl pkcs12 -in demo.p12 -nocerts -nodes -out demo.key
匯出私鑰
openssl rsa -in demo.key -out demo_pri.pem
匯出公鑰
openssl rsa -in demo.key -pubout -out demo_pub.pem