python自动生成公私钥

pycryptodome模块安装:

pip install pycryptodome==3.10.1

实例:通过python自动生成公私钥

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from Crypto import Random
from Crypto.PublicKey import RSA

random_generator = Random.new().read
key = RSA.generate(2048, random_generator)

# 生成私钥
f = open('private.rsa', 'w')
f.write(key.exportKey().decode('utf-8'))
f.close()

# 生成公钥
f = open('public.rsa', 'w')
f.write(key.publickey().exportKey().decode('utf-8'))
f.close()

PyCryptodome模块

上一篇:用 Hypothesis 来自动化单元测试


下一篇:Python encode()和decode()方法:字符串编码转换