一直听别人说RSA算法加密比较难,今天也试着实践下
import rsa
import base64
import os
def generateRsa():
f, e = rsa.newkeys(2048) # 生成公钥、私钥
e = e.save_pkcs1()
with open("e.pem", "wb") as x:
x.write(e)
f = f.save_pkcs1()
with open("f.pem", "wb") as x:
x.write(f)
# rsa加密
def rsaEncrypt(biz_content):
# 读取公钥文件
with open("f.pem", "rb") as x:
pubkey = x.read()
pubkey = rsa.PublicKey.load_pkcs1(pubkey)
biz_content = biz_content.encode('utf-8')
# 2048bit key
default_encrypt_length = 245
len_content = len(biz_content)
res = []
for i in range(0, len_content, default_encrypt_length):
res.append(rsa.encrypt(biz_content[i:i + default_encrypt_length],pubkey))
byte_data = b''.join(res)
return base64.b64encode(byte_data)
# rsa解密
def rsaDecrypt(biz_content):
with open("e