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中的__SLOTS__属性使用示例
Feb 18 Python
在Django中使用Sitemap的方法讲解
Jul 22 Python
使用Python实现BT种子和磁力链接的相互转换
Nov 09 Python
Python 编码Basic Auth使用方法简单实例
May 25 Python
python贪婪匹配以及多行匹配的实例讲解
Apr 19 Python
Python制作exe文件简单流程
Jan 24 Python
python ---lambda匿名函数介绍
Mar 13 Python
python tkinter控件布局项目实例
Nov 04 Python
Python实现投影法分割图像示例(二)
Jan 17 Python
pycharm 的Structure界面设置操作
Feb 05 Python
pytorch 中nn.Dropout的使用说明
May 20 Python
Python之基础函数案例详解
Aug 30 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
基于curl数据采集之单页面采集函数get_html的使用
2013/04/28 PHP
input、button的不同type值在ajax提交表单时导致的陷阱
2009/02/24 Javascript
jQuery EasyUI NumberBox(数字框)的用法
2010/07/08 Javascript
今天是星期几的4种JS代码写法
2013/09/17 Javascript
用js控制组织结构图可以任意拖拽到指定位置
2014/01/17 Javascript
jquery分析文本里url或邮件地址为真实链接的方法
2015/06/20 Javascript
JS实现弹出浮动窗口(支持鼠标拖动和关闭)实例详解
2015/08/06 Javascript
jquery实现简洁文件上传表单样式
2015/11/02 Javascript
JavaScript位置与大小(1)之正确理解和运用与尺寸大小相关的DOM属性
2015/12/26 Javascript
原生js获取浏览器窗口及元素宽高常用方法集合
2017/01/18 Javascript
Vue2.0使用过程常见的一些问题总结学习
2017/04/10 Javascript
微信小程序实现图片滚动效果示例
2018/12/05 Javascript
详解element-ui中form验证杂记
2019/03/04 Javascript
Moment.js实现多个同时倒计时
2019/08/26 Javascript
Js和VUE实现跑马灯效果
2020/05/25 Javascript
[29:16]完美世界DOTA2联赛决赛日 Inki vs LBZS 第三场 11.08
2020/11/10 DOTA
python让图片按照exif信息里的创建时间进行排序的方法
2015/03/16 Python
django反向解析和正向解析的方式
2018/06/05 Python
Python正则表达式和re库知识点总结
2019/02/11 Python
python 使用pdfminer3k 读取PDF文档的例子
2019/08/27 Python
python使用docx模块读写docx文件的方法与docx模块常用方法详解
2020/02/17 Python
python对数组进行排序,并输出排序后对应的索引值方式
2020/02/28 Python
如何将PySpark导入Python的放实现(2种)
2020/04/26 Python
50道外企软件测试面试题
2014/08/18 面试题
新闻记者实习自我鉴定
2013/09/19 职场文书
百度吧主申请感言
2014/01/12 职场文书
学生打架检讨书1000字
2014/01/16 职场文书
电子商务个人职业生涯规划范文
2014/02/12 职场文书
英文求职信范文
2014/05/23 职场文书
欢迎横幅标语
2014/06/17 职场文书
房屋财产继承协议书范本
2014/11/03 职场文书
作文评语集锦
2014/12/25 职场文书
运动会新闻稿
2015/07/17 职场文书
2016年圣诞节活动总结范文
2016/04/01 职场文书
创业计划书之家教托管
2019/09/25 职场文书
Go语言切片前或中间插入项与内置copy()函数详解
2021/04/27 Golang