pycryptoの使い方
colab
RSA
秘密鍵の作成
import Crypto
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
print(key.exportKey("PEM").decode())
# 保存
with open("private.pem", "wb") as fp:
fp.write(key.exportKey("PEM"))
# 読み込み
key = RSA.importKey(open("private.pem", "rb").read())
公開鍵の作成
publickey = key.publickey()
with open("publickey.pem", "wb") as fp:
fp.write(publickey.exportKey("PEM"))
publickey = RSA.importKey(open("publickey.pem", "rb").read())
print(open("publickey.pem").read())
公開鍵で暗号化し秘密鍵で復号化
himitsu = publickey.encrypt(bytes("あいうえお", "utf-8"), 32)
himitsu
key.decrypt(himitsu).decode()