一.建立pkcs8格式的RSA私鑰和和公鑰
步驟如下:
1.生成 RSA 私鑰
openssl genRSA -out RSAprivatekey.pem 1024
2.生成對應的公鑰
openssl RSA -in RSAprivatekey.pem -pubout -out RSApublickey.pem
3.将 RSA 私鑰轉換成 PKCS8 格式,
openssl pkcs8 -topk8 -inform PEM -in RSAprivatekey.pem -outform PEM -nocrypt -out RSAprivatepkcs8.pem
4.生成的RSAprivatepkcs8.pem/RSApublickey.pem 需要提取key檔案部分并且将key合并成一行生成新的公鑰和私鑰, 這對密鑰就可以用了.即生成了字尾名為pem格式的Rsa密鑰.
$ cat merge.py
import sys
fname = sys.argv[1]
f=open(fname)
total=''
for l in f.readlines():
if l.startswith('----'):
continue
total = total + l.strip()
print(total)
二.建立pkcs12格式的RSA證書
今天生成pkcs12格式的RSA密鑰老出問題,在把公鑰和私鑰合并生成.p12證書的時候,一直出錯提示
unable to load certificates,後來重新找生成2048 bit的就沒有問題了,之前生成的都是1024 bit的.生成過程很
簡單.
具體步驟如下:
1.運作以下OpenSSL指令生成私鑰和公鑰證書(運作以下OpenSSL指令生成私鑰和公共證書)
(certificate.pem為公鑰,key.pem為私鑰)
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem
2.檢視自己建立的公鑰證書
openssl x509 -text -noout -in certificate.pem
3.合并公鑰和私鑰生成PKCS 12證書:
openssl pkcs12 -inkey key.pem -in certificate.pem -export -out certificate.p12
4.校驗PKCS 12證書密碼
openssl pkcs12 -in certificate.p12 -noout -info
到此PKCS 12證書檔案就生成好了,直接在windows下輕按兩下輸入密碼進行導入後,就可以在愛IE浏覽器下的證書下面看見了.例如:

三.從pkcs12RSA證書擷取RSA私鑰和和公鑰
1.擷取pem字尾公鑰:
openssl pkcs12 -in yourP12File -clcerts -nokeys -out publicCert.pem
2.先把p12證書安裝導入到IE浏覽器下,再從IE中導出公鑰.支援的格式有CER,P7B格式.
3.擷取pem字尾私鑰:
openssl pkcs12 -in yourP12File -nocerts -out privateKey.pem
查閱:
https://www.ibm.com/support/knowledgecenter/en/SSWHYP_4.0.0/com.ibm.apimgmt.cmc.doc/task_apionprem_gernerate_self_signed_openSSL.html