生成公钥、私钥
from Crypto import Random
from Crypto.PublicKey import RSA
# 获取一个伪随机数生成器
random_generator = Random.new().read
# 获取一个rsa算法对应的密钥对生成器实例
rsa = RSA.generate(2048, random_generator)
# 生成私钥并保存
private_pem = rsa.exportKey()
with open('rsa.key', 'wb') as f:
f.write(private_pem)
# 生成公钥并保存
public_pem = rsa.publickey().exportKey()
with open('rsa.pub', 'wb') as f:
f.write(public_pem)
长字符串加密
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
import base64
import sys
def read_file(input_file):
with open(input_file, 'r') as f:
message = f.read()
return message
pass
def encrypt_file(message, pubkey_file, out_file):
with open(pubkey_file, 'r') as f:
publicKey = f.read()
pubKeyObj