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继承和抽象类的实现方法
Jan 14 Python
浅谈Python中数据解析
May 05 Python
在windows下快速搭建web.py开发框架方法
Apr 22 Python
使用Python对SQLite数据库操作
Apr 06 Python
详解Python中类的定义与使用
Apr 11 Python
python中安装模块包版本冲突问题的解决
May 02 Python
Python中return self的用法详解
Jul 27 Python
Laravel+Dingo/Api 自定义响应的实现
Feb 17 Python
python基于递归解决背包问题详解
Jul 03 Python
python连接PostgreSQL过程解析
Feb 09 Python
python DataFrame中stack()方法、unstack()方法和pivot()方法浅析
Apr 06 Python
python神经网络 使用Keras构建RNN训练
May 04 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同时支持GIF、png、JPEG
2006/10/09 PHP
在普通HTTP上安全地传输密码
2007/07/21 PHP
php实现文件下载(支持中文文名)
2013/12/04 PHP
Fedora下安装php Redis扩展笔记
2014/09/03 PHP
分享ThinkPHP3.2中关联查询解决思路
2015/09/20 PHP
PHP中addslashes与mysql_escape_string的区别分析
2016/04/25 PHP
PHP实现带重试功能的curl连接示例
2016/07/28 PHP
javascript倒计时功能实现代码
2012/06/07 Javascript
javascript通过className来获取元素的简单示例代码
2014/01/10 Javascript
JavaScript中变量声明有var和没var的区别示例介绍
2014/09/15 Javascript
轻松创建nodejs服务器(3):代码模块化
2014/12/18 NodeJs
js实现表单检测及表单提示的方法
2015/08/14 Javascript
Bootstrap入门书籍之(一)排版
2016/02/17 Javascript
快速掌握Node.js中setTimeout和setInterval的使用方法
2016/03/21 Javascript
webpack下实现动态引入文件方法
2018/02/22 Javascript
vuex与组件联合使用的方法
2018/05/10 Javascript
jsonp跨域获取百度联想词的方法分析
2019/05/13 Javascript
jquery轮播图插件使用方法详解
2020/07/31 jQuery
uin-app+mockjs实现本地数据模拟
2020/08/26 Javascript
[06:06]2018DOTA2亚洲邀请赛主赛事第四日战况回顾 全明星赛欢乐上演
2018/04/07 DOTA
Python简单的制作图片验证码实例
2017/05/31 Python
Python实现多级目录压缩与解压文件的方法
2018/09/01 Python
python批量识别图片指定区域文字内容
2019/04/30 Python
Python实现平行坐标图的绘制(plotly)方式
2019/11/22 Python
Numpy(Pandas)删除全为零的列的方法
2020/09/11 Python
实现CSS3中的border-radius(边框圆角)示例代码
2013/07/19 HTML / CSS
学生个人的自我评价分享
2013/11/05 职场文书
教师的实习自我鉴定
2013/12/17 职场文书
人力资源部副职的竞聘演讲稿
2014/01/07 职场文书
三年级数学教学反思
2014/01/31 职场文书
导游词之云南丽江古城
2019/09/17 职场文书
go语言中json数据的读取和写出操作
2021/04/28 Golang
Python Django框架介绍之模板标签及模板的继承
2021/05/27 Python
Python趣味爬虫之用Python实现智慧校园一键评教
2021/05/28 Python
浅析Python中的套接字编程
2021/06/22 Python
排查并解决Oracle sysaux表空间异常增长
2022/04/20 Oracle