Python3非对称加密算法RSA实例详解


Posted in Python onDecember 06, 2018

本文实例讲述了Python3非对称加密算法RSA。分享给大家供大家参考,具体如下:

python3 可以使用 Crypto.PublicKey.RSA 和 rsa 生成公钥、私钥。

其中 python3.6 Crypto 库的安装方式请参考前面一篇《Python3对称加密算法AES、DES3》

rsa 加解密的库使用 pip3 install rsa 就行了

C:\WINDOWS\system32>pip3 install rsa
Collecting rsa
  Downloading https://files.pythonhosted.org/packages/e1/ae/baedc9cb175552e95f3395c43055a6a5e125ae4d48a1d7a924baca83e92e/rsa-3.4.2-py2.py3-none-any.whl (46kB)
    100% |????????????????????????????????| 51kB 99kB/s
Collecting pyasn1>=0.1.3 (from rsa)
  Downloading https://files.pythonhosted.org/packages/a0/70/2c27740f08e477499ce19eefe05dbcae6f19fdc49e9e82ce4768be0643b9/pyasn1-0.4.3-py2.py3-none-any.whl (72kB)
    100% |????????????????????????????????| 81kB 289kB/s
Installing collected packages: pyasn1, rsa
Successfully installed pyasn1-0.4.3 rsa-3.4.2

使用 Crypto.PublicKey.RSA 生成公钥、私钥:

import Crypto.PublicKey.RSA
import Crypto.Random
x = Crypto.PublicKey.RSA.generate(2048)
a = x.exportKey("PEM") # 生成私钥
b = x.publickey().exportKey()  # 生成公钥
with open("a.pem", "wb") as x:
  x.write(a)
with open("b.pem", "wb") as x:
  x.write(b)
y = Crypto.PublicKey.RSA.generate(2048, Crypto.Random.new().read)  # 使用 Crypto.Random.new().read 伪随机数生成器
c = y.exportKey()  # 生成私钥
d = y.publickey().exportKey()  #生成公钥
with open("c.pem", "wb") as x:
  x.write(c)
with open("d.pem", "wb") as x:
  x.write(d)

使用 Crypto.PublicKey.RSA.importKey(private_key) 生成公钥和证书:

import Crypto.PublicKey.RSA
with open("a.pem", "rb") as x:
  xx = Crypto.PublicKey.RSA.importKey(x.read())
b = xx.publickey().exportKey()  # 生成公钥
with open("b.pem", "wb") as x:
  x.write(b)
a = xx.exportKey("DER")  # 生成 DER 格式的证书
with open("a.der", "wb") as x:
  x.write(a)

使用 rsa 生成公钥、私钥:

import rsa
f, e = rsa.newkeys(2048)  # 生成公钥、私钥
e = e.save_pkcs1() # 保存为 .pem 格式
with open("e.pem", "wb") as x: # 保存私钥
  x.write(e)
f = f.save_pkcs1() # 保存为 .pem 格式
with open("f.pem", "wb") as x: # 保存公钥
  x.write(f)

RSA非对称加密算法实现:

使用Crypto模块:

import Crypto.PublicKey.RSA
import Crypto.Cipher.PKCS1_v1_5
import Crypto.Random
import Crypto.Signature.PKCS1_v1_5
import Crypto.Hash
y = b"abcdefg1234567"
with open("b.pem", "rb") as x:
  b = x.read()
  cipher_public = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(b))
  cipher_text = cipher_public.encrypt(y) # 使用公钥进行加密
with open("a.pem", "rb") as x:
  a = x.read()
  cipher_private = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(a))
  text = cipher_private.decrypt(cipher_text, Crypto.Random.new().read)  # 使用私钥进行解密
assert text == y  # 断言验证
with open("c.pem", "rb") as x:
  c = x.read()
  c_rsa = Crypto.PublicKey.RSA.importKey(c)
  signer = Crypto.Signature.PKCS1_v1_5.new(c_rsa)
  msg_hash = Crypto.Hash.SHA256.new()
  msg_hash.update(y)
  sign = signer.sign(msg_hash)  # 使用私钥进行'sha256'签名
with open("d.pem", "rb") as x:
  d = x.read()
  d_rsa = Crypto.PublicKey.RSA.importKey(d)
  verifer = Crypto.Signature.PKCS1_v1_5.new(d_rsa)
  msg_hash = Crypto.Hash.SHA256.new()
  msg_hash.update(y)
  verify = verifer.verify(msg_hash, sign) # 使用公钥验证签名
  print(verify)

运行结果:

True

使用 rsa 模块:

import rsa
y = b"abcdefg1234567"
with open("e.pem", "rb") as x:
  e = x.read()
  e = rsa.PrivateKey.load_pkcs1(e)  # load 私钥
with open("f.pem", "rb") as x:
  f = x.read()
  f = rsa.PublicKey.load_pkcs1(f) # load 公钥,由于之前生成的私钥缺少'RSA'字段,故无法 load
cipher_text = rsa.encrypt(y, f) # 使用公钥加密
text = rsa.decrypt(cipher_text, e)  # 使用私钥解密
assert text == y  # 断言验证
sign = rsa.sign(y, e, "SHA-256") # 使用私钥进行'sha256'签名
verify = rsa.verify(y, sign, f) # 使用公钥验证签名
print(verify)

运行结果:

True

Python 相关文章推荐
python实现zencart产品数据导入到magento(python导入数据)
Apr 03 Python
python利用urllib实现爬取京东网站商品图片的爬虫实例
Aug 24 Python
python2.7无法使用pip的解决方法(安装easy_install)
Apr 03 Python
Python下使用Scrapy爬取网页内容的实例
May 21 Python
Python面向对象之接口、抽象类与多态详解
Aug 27 Python
Python使用pyautogui模块实现自动化鼠标和键盘操作示例
Sep 04 Python
python 常见字符串与函数的用法详解
Nov 23 Python
华为2019校招笔试题之处理字符串(python版)
Jun 25 Python
django 消息框架 message使用详解
Jul 22 Python
python实现的生成word文档功能示例
Aug 23 Python
小结Python的反射机制
Sep 28 Python
pandas中DataFrame重置索引的几种方法
May 24 Python
Python3对称加密算法AES、DES3实例详解
Dec 06 #Python
Python http接口自动化测试框架实现方法示例
Dec 06 #Python
python的常用模块之collections模块详解
Dec 06 #Python
Django管理员账号和密码忘记的完美解决方法
Dec 06 #Python
Python操作json的方法实例分析
Dec 06 #Python
Python多线程应用于自动化测试操作示例
Dec 06 #Python
Python实现多属性排序的方法
Dec 05 #Python
You might like
使用sockets:从新闻组中获取文章(三)
2006/10/09 PHP
ajax+php打造进度条 readyState各状态
2010/03/20 PHP
php的ajax框架xajax入门与试用介绍
2010/12/19 PHP
PHP+memcache实现消息队列案例分享
2014/05/21 PHP
非常全面的php日期时间运算汇总
2015/11/04 PHP
php微信浏览器分享设置以及回调详解
2016/08/01 PHP
Yii2框架数据库简单的增删改查语法小结
2016/08/31 PHP
简单的无缝滚动程序-仅几行代码
2007/05/08 Javascript
js树形控件脚本代码
2008/07/24 Javascript
Javascript 去除数组的重复元素
2010/05/04 Javascript
jquery中的 $("#jb51")与document.getElementById("jb51") 的区别
2011/07/26 Javascript
javascript:history.go()和History.back()的区别及应用
2012/11/25 Javascript
jQuery 属性选择器element[herf*='value']使用示例
2013/10/20 Javascript
js中的eventType事件及其浏览器支持性介绍
2013/11/29 Javascript
jQuery插件multiScroll实现全屏鼠标滚动切换页面特效
2015/04/12 Javascript
纯js模拟div层弹性运动的方法
2015/07/27 Javascript
分享五个有用的jquery小技巧
2015/10/08 Javascript
Javascript之String对象详解
2016/06/08 Javascript
input获取焦点时底部菜单被顶上来问题的解决办法
2017/01/24 Javascript
Vue服务器渲染Nuxt学习笔记
2018/01/31 Javascript
分享5个顶级的JavaScript Ajax组件库
2018/09/16 Javascript
vue 计算属性和侦听器的使用小结
2021/01/25 Vue.js
用Javascript实现发送短信验证码间隔功能
2021/02/08 Javascript
python动态监控日志内容的示例
2014/02/16 Python
python中os操作文件及文件路径实例汇总
2015/01/15 Python
Python实现对百度云的文件上传(实例讲解)
2017/10/21 Python
通过python+selenium3实现浏览器刷简书文章阅读量
2017/12/26 Python
Urban Outfitters美国官网:美国生活方式品牌
2016/08/26 全球购物
学校食堂采购员岗位职责
2013/12/05 职场文书
弘扬雷锋精神演讲稿
2014/05/10 职场文书
安全隐患整改报告
2014/11/06 职场文书
2015年环卫工作总结
2015/04/28 职场文书
学习习近平主席讲话心得体会
2016/01/20 职场文书
2016年感恩节活动总结大全
2016/04/01 职场文书
用人单位的规章制度,怎样制定才是有效的?
2019/07/09 职场文书
详解CSS中postion和opacity及cursor的特性
2022/08/14 HTML / CSS