python rsa模块【sign 加签验签】的使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python rsa模块【sign 加签验签】的使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2500字,纯文字阅读大概需要4分钟。
内容图文
![python rsa模块【sign 加签验签】的使用](/upload/InfoBanner/zyjiaocheng/1173/cc7a40f2d32a4110ae3eea251fc98b38.jpg)
https://www.cnblogs.com/kayb/p/8157556.html
https://www.jianshu.com/p/518fa5d59f89
**https://blog.csdn.net/ctwy291314/article/details/88822130
公钥加密、私钥解密
1 # -*- coding: utf-8 -*- 2import rsa 3 4# rsa加密 5def rsaEncrypt(str): 6 # 生成公钥、私钥 7 (pubkey, privkey) = rsa.newkeys(512) 8 print("pub: ", pubkey) 9 print("priv: ", privkey) 10 # 明文编码格式 11 content = str.encode(‘utf-8‘) 12 # 公钥加密 13 crypto = rsa.encrypt(content, pubkey) 14return (crypto, privkey) 151617# rsa解密 18def rsaDecrypt(str, pk): 19 # 私钥解密 20 content = rsa.decrypt(str, pk) 21 con = content.decode(‘utf-8‘) 22return con 232425 (a, b) = rsaEncrypt("hello") 26 print(‘加密后密文:‘) 27print(a) 28 content = rsaDecrypt(a, b) 29 print(‘解密后明文:‘) 30 print(content)
密钥导出、签名验证
1 # -*- coding: utf-8 -*- 2import rsa 3 4# 先生成一对密钥,然后保存.pem格式文件,当然也可以直接使用 5 (pubkey, privkey) = rsa.newkeys(1024) 6 7 pub = pubkey.save_pkcs1() 8 pubfile = open(‘public.pem‘, ‘wb‘) 9pubfile.write(pub) 10pubfile.close() 1112 pri = privkey.save_pkcs1() 13 prifile = open(‘private.pem‘, ‘wb‘) 14prifile.write(pri) 15prifile.close() 1617# load公钥和密钥 18 message = ‘lovesoo.org‘19 with open(‘public.pem‘, "rb") as publickfile: 20 p = publickfile.read() 21 pubkey = rsa.PublicKey.load_pkcs1(p) 22 print(pubkey) 23 with open(‘private.pem‘, "rb") as privatefile: 24 p = privatefile.read() 25 privkey = rsa.PrivateKey.load_pkcs1(p) 26 print(privkey) 27# 用公钥加密、再用私钥解密 28 crypto = rsa.encrypt(message.encode(‘utf-8‘), pubkey) 29 message = rsa.decrypt(crypto, privkey) 30 message = message.decode(‘utf-8‘) 31print (message) 3233# sign 用私钥签名认证、再用公钥验证签名 34 signature = rsa.sign(message.encode(‘utf-8‘), privkey, ‘SHA-1‘) 35 method_name = rsa.verify(‘lovesoo.org‘.encode(‘utf-8‘), signature, pubkey) 36 print(method_name)
对文件进行RSA加密解密
from rsa.bigfile import * import rsa with open(‘public.pem‘) as publickfile: p = publickfile.read() pubkey = rsa.PublicKey.load_pkcs1(p) with open(‘private.pem‘) as privatefile: p = privatefile.read() privkey = rsa.PrivateKey.load_pkcs1(p) with open(‘mysec.txt‘, ‘rb‘) as infile, open(‘outputfile‘, ‘wb‘) as outfile: #加密输出 encrypt_bigfile(infile, outfile, pubkey) with open(‘outputfile‘, ‘rb‘) as infile2, open(‘result‘, ‘wb‘) as outfile2: #解密输出 decrypt_bigfile(infile2, outfile2, privkey)
4.0的主要变化
版本3.4是3.x范围内的最后一个版本。版本4.0删除了以下模块,因为它们不安全:
rsa._version133
rsa._version200
rsa.bigfile
rsa.varblock
这些模块在3.4版中被标记为已弃用。
此外,在4.0中,I / O函数经过简化,可以在所有支持的Python版本上使用字节。
4.0版本不再支持Python 2.6和3.3。
原文:https://www.cnblogs.com/forforever/p/12870461.html
内容总结
以上是互联网集市为您收集整理的python rsa模块【sign 加签验签】的使用全部内容,希望文章能够帮你解决python rsa模块【sign 加签验签】的使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。