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 threading多线程编程实例
Sep 18 Python
python脚本设置系统时间的两种方法
Feb 21 Python
python虚拟环境的安装配置图文教程
Oct 20 Python
基于numpy.random.randn()与rand()的区别详解
Apr 17 Python
Python父目录、子目录的相互调用方法
Feb 16 Python
Python实现简单查找最长子串功能示例
Feb 26 Python
Python中捕获键盘的方式详解
Mar 28 Python
Django使用中间件解决前后端同源策略问题
Sep 02 Python
python字符串替换re.sub()方法解析
Sep 18 Python
Python unittest单元测试框架及断言方法
Apr 15 Python
Python selenium爬取微博数据代码实例
May 22 Python
scrapy实践之翻页爬取的实现
Jan 05 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实现接收二进制流转换成图片的方法
2017/01/10 PHP
js类 from qq
2006/11/13 Javascript
Javascript公共脚本库系列(一): 弹出层脚本
2011/02/24 Javascript
Prototype源码浅析 Enumerable部分(二)
2012/01/18 Javascript
js兼容的placeholder属性详解
2013/08/18 Javascript
关于IE中getElementsByClassName不能用的问题解决方法
2013/08/26 Javascript
jqeury-easyui-layout问题解决方法
2014/03/24 Javascript
JavaScript常用验证函数实例汇总
2014/11/25 Javascript
PHPExcel中的一些常用方法汇总
2015/01/23 Javascript
jQuery实现获取h1-h6标题元素值的方法
2017/03/06 Javascript
Underscore之Array_动力节点Java学院整理
2017/07/10 Javascript
vue 2.0 购物车小球抛物线的示例代码
2018/02/01 Javascript
原生JS实现动态添加新元素、删除元素方法
2019/05/05 Javascript
vue 解决setTimeOut和setInterval函数无效报错的问题
2020/07/30 Javascript
react中hook介绍以及使用教程
2020/12/11 Javascript
[00:47]TI7不朽珍藏III——沙王不朽展示
2017/07/15 DOTA
Python在不同目录下导入模块的实现方法
2017/10/27 Python
python将文本分每两行一组并保存到文件
2018/03/19 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
2018/10/28 Python
python中np是做什么的
2020/07/21 Python
Python定时任务框架APScheduler原理及常用代码
2020/10/05 Python
使用CSS3设计地图上的雷达定位提示效果
2016/04/05 HTML / CSS
西尔斯百货官网:Sears
2016/09/06 全球购物
英国乐购杂货:Tesco Groceries
2018/11/29 全球购物
NICKIS.com荷兰:设计师儿童时装
2020/01/08 全球购物
端口镜像是怎么实现的
2014/03/25 面试题
求职简历自荐信
2013/10/20 职场文书
大学自我鉴定范文
2013/12/26 职场文书
铁路工务反思材料
2014/02/07 职场文书
医院护士见习期自我鉴定
2014/04/10 职场文书
教师党员岗位承诺书
2014/05/29 职场文书
班级口号大全
2014/06/09 职场文书
新闻专业毕业生求职信
2014/08/08 职场文书
游戏开发中如何使用CocosCreator进行音效处理
2021/04/14 Javascript
go语言中切片与内存复制 memcpy 的实现操作
2021/04/27 Golang
Python基础之进程详解
2021/05/21 Python