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开启多个子进程并行运行的方法
Apr 18 Python
PyQt5实现下载进度条效果
Apr 19 Python
python实现超市扫码仪计费
May 30 Python
python面向对象多线程爬虫爬取搜狐页面的实例代码
May 31 Python
Python设计模式之抽象工厂模式原理与用法详解
Jan 15 Python
对Python _取log的几种方式小结
Jul 25 Python
Python如何应用cx_Oracle获取oracle中的clob字段问题
Aug 27 Python
Python 通过监听端口实现唯一脚本运行方式
May 05 Python
利用Python实现Excel的文件间的数据匹配功能
Jun 16 Python
python爬虫中PhantomJS加载页面的实例方法
Nov 12 Python
用python画城市轮播地图
May 28 Python
Python绘画好看的星空图
Mar 17 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
用php实现像JSP,ASP里Application那样的全局变量
2007/01/12 PHP
mysql_fetch_assoc和mysql_fetch_row的功能加起来就是mysql_fetch_array
2007/01/15 PHP
php的日期处理函数及uchome的function_coomon中日期处理函数的研究
2011/01/12 PHP
php模仿asp Application对象在线人数统计实现方法
2015/01/04 PHP
PHP数组相关函数汇总
2015/03/24 PHP
PHP常用header头定义代码示例汇总
2020/08/29 PHP
JSON 学习之完全手册 图文
2007/05/29 Javascript
基于jquery的finkyUI插件与Ajax实现页面数据加载功能
2010/12/03 Javascript
JQuery判断HTML元素是否存在的两种解决方法
2013/12/26 Javascript
jQuery关键词说明插件cluetip使用指南
2015/04/21 Javascript
在JavaScript中操作数组之map()方法的使用
2015/06/09 Javascript
基于jQuery滑动杆实现购买日期选择效果
2015/09/15 Javascript
jQuery基本选择器和层次选择器学习使用
2017/02/27 Javascript
jQuery中hover方法搭配css的hover选择器,实现选中元素突出显示方法
2017/05/08 jQuery
JavaScript算法教程之sku(库存量单位)详解
2017/06/29 Javascript
使用JQ完成表格隔行换色的简单实例
2017/08/25 Javascript
jQuery实现对网页节点的增删改查功能示例
2017/09/18 jQuery
解决vuejs项目里css引用背景图片不能显示的问题
2018/09/13 Javascript
Vue运用transition实现过渡动画
2019/05/06 Javascript
微信小程序云开发(数据库)详解
2019/05/17 Javascript
Python 随机生成中文验证码的实例代码
2013/03/20 Python
Python采集猫眼两万条数据 对《无名之辈》影评进行分析
2018/12/05 Python
Django打印出在数据库中执行的语句问题
2019/07/25 Python
python 已知三条边求三角形的角度案例
2020/04/12 Python
is_file和file_exists效率比较
2021/03/14 PHP
屈臣氏马来西亚官网:Watsons马来西亚
2019/06/15 全球购物
两只小狮子教学反思
2014/02/05 职场文书
人事专员工作职责
2014/02/22 职场文书
关于环保的建议书400字
2014/03/12 职场文书
期末评语大全
2014/05/04 职场文书
股东出资证明书范例
2014/10/04 职场文书
大二学生自我检讨书
2014/10/23 职场文书
2015年“我们的节日·重阳节”活动总结
2015/07/29 职场文书
python Tkinter的简单入门教程
2021/04/11 Python
详解CocosCreator项目结构机制
2021/04/14 Javascript
Python内置数据结构列表与元组示例详解
2021/08/04 Python