import rsa
from binascii import b2a_hex, a2b_hex
#加密函数
def encrypt(text):
# 计算文本数量
i_num = sheng_num(text)
# 获取密钥
pubkey, prikey = rsa.newkeys(i_num)
#加密字符串
ciphertext = rsa.encrypt(text.encode("utf-8"), pubkey)
# 把加密后的字符串转化为16进制字符串
return b2a_hex(ciphertext),prikey
#计算
def sheng_num(text):
for i in range(len(text)* 3):
if i*8-11 >= len(text)* 3:break
return i*8*8
#解密函数
def decrypt(text,prikey):
decrypt_text = rsa.decrypt(a2b_hex(text), prikey)
return decrypt_text.decode()
#代码
text = '张三李四强强侵权张三李四强强侵权张三李四强强侵权张三李四强强侵权张三李四强强侵权张三李四强强侵权张三李四强强侵权张三李四强强侵权张三李四强强侵权张三李四强强侵权'
ency_text,prikey = encrypt(text)
print("加密后:%s" % ency_text)
decy_text = decrypt(ency_text,prikey)
print("解密后:%s" % decy_text)