Cipher_pkcs1_v1_5直接加密长字符串问题

Cipher_pkcs1_v1_5直接加密短字符串没有问题,但是加密长字符串就会报错,需要进行分块加密,分块解密

非分段加密

def encrypt(message):
“”"
RSA加密
:param message:
“”"
with open(‘private.pem’) as f:
key = f.read()
rsakey = RSA.importKey(key)
cipher = Cipher_pkcs1_v1_5.new(rsakey)
cipher_text = binascii.b2a_base64(cipher.encrypt(message))
return cipher_text

非分段解密

def decrypt(cipher_text):
“”“RSA解密步骤:
1.对cipher_text进行base64解码:binascii.a2b_base64(cipher_text)
2.对上一步结果RSA解密:cipher.decrypt()
:param cipher_text:
“””
with open(‘publics.pem’) as f:
key = f.read()
rsakey = RSA.importKey(key)
cipher = Cipher_pkcs1_v1_5.new(rsakey)
text = cipher.decrypt(binascii.a2b_base64(cipher_text), b’xyz’)
# 解决16进制utf-8编码的中文乱码问题
a = bytes(text).decode(‘utf-8’)
return a
运行结果:
栗子

你可能感兴趣的:(Cipher_pkcs1_v1_5直接加密长字符串问题)