Create_RSA_Key.py
Last updated
Last updated
新增 RSA 私鑰與公鑰,並將公私鑰內容寫入 .pem 二進制檔案中,如同下圖。
from Crypto.PublicKey import RSA
'''
=========================================================
Author: Skylar Ma
---------------------------------------------------------
Description: Create RSA private key and primary key.
---------------------------------------------------------
Version Date Note
1.0 2024-01-27 Init.
=========================================================
'''
# 產生 2048 位元 RSA 金鑰
key = RSA.generate(2048)
# RSA 私鑰
privateKey = key.export_key()
with open("private.pem", "wb") as f:
f.write(privateKey)
# RSA 公鑰
publicKey = key.publickey().export_key()
with open("public.pem", "wb") as f:
f.write(publicKey)