# 加密 @classmethod defrsa_encrypt(cls, message, p_length=200): rsakey = RSA.importKey(open(cls.public_pem).read()) cipher = Cipher_pkcs1_v1_5.new(rsakey) # 创建用于执行pkcs1_v1_5加密或解密的密码 res = [] for i inrange(0, len(message), p_length): res.append(cipher.encrypt(message[i:i+p_length].encode())) cipher_text_byte = b'' for b in res: cipher_text_byte += b cipher_text = base64.b64encode(cipher_text_byte) return cipher_text.decode('utf-8')
# 解密 @classmethod defrsa_decrypt(cls, cipher_text, p_length=256): encrypt_text = base64.b64decode(cipher_text.encode('utf-8')) rsakey = RSA.importKey(open(cls.private_pem).read()) cipher = Cipher_pkcs1_v1_5.new(rsakey) # 创建用于执行pkcs1_v1_5加密或解密的密码 res = [] for i inrange(0, len(encrypt_text), p_length): res.append(cipher.decrypt(encrypt_text[i:i+p_length], "解密失败")) text = b'' for b in res: text += b return text.decode('utf-8')