rsa包中现成的公钥加密、私钥解密代码
import rsa
def rsa_encrypt(d_str):
# 生成公钥和私钥
pubkey, privkey = rsa.newkeys(1024)
# 将字符串进行编码
content = d_str.encode('utf-8')
# 公钥加密
crypto = rsa.encrypt(content, pubkey)
print ('加密后', crypto)
return crypto, privkey
def rsa_decrypt(crypto, privkey):
# 解密
content = rsa.decrypt(crypto, privkey)
# 解码
content = content.decode('utf-8')
print ('解密结果', content)
if __name__ == '__main__':
a = rsa_encrypt('hello word')
rsa_decrypt(*a)
但没有现成的私钥加密、公钥解密的实现
通过阅读源码,整理出一份简易的实现方式
import rsa
from rsa import common, transform, core
import os
def _pad_for_encryption(message, target_length):
max_msglength = target_length - 11
msglength = len(message)
if msglength > max_msglength: