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学习小技巧之利用字典的默认行为
May 20 Python
python实现在pandas.DataFrame添加一行
Apr 04 Python
详解Python的数据库操作(pymysql)
Apr 04 Python
Python Flask 搭建微信小程序后台详解
May 06 Python
PyCharm如何导入python项目的方法
Feb 06 Python
python实现音乐播放和下载小程序功能
Apr 26 Python
Python正则表达式如何匹配中文
May 27 Python
使用keras框架cnn+ctc_loss识别不定长字符图片操作
Jun 29 Python
Python pip安装第三方库实现过程解析
Jul 09 Python
一文读懂Python 枚举
Aug 25 Python
如何使用pycharm连接Databricks的步骤详解
Sep 23 Python
Pycharm安装python库的方法
Nov 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
PHP 读取文件的正确方法
2009/04/29 PHP
将FCKeditor导入PHP+SMARTY的实现方法
2015/01/15 PHP
在laravel框架中实现封装公共方法全局调用
2019/10/14 PHP
不使用中间变量,交换int型的 a, b两个变量的值。
2010/10/29 Javascript
javascript获取dom的下一个节点方法
2014/09/05 Javascript
javascript中undefined与null的区别
2015/08/16 Javascript
js调用百度地图及调用百度地图的搜索功能
2015/09/07 Javascript
利用JavaScript阻止表单提交的两种方法
2016/08/11 Javascript
微信公众号  提示:Unauthorized API function 问题解决方法
2016/12/05 Javascript
javascript事件的绑定基础实例讲解(34)
2017/02/14 Javascript
JavaScript和jQuery制作光棒效果
2017/02/24 Javascript
JavaScript运动框架 多值运动(四)
2017/05/18 Javascript
详谈表单重复提交的三种情况及解决方法
2017/08/16 Javascript
js使用xml数据载体实现城市省份二级联动效果
2017/11/08 Javascript
js原生实现移动端手指滑动轮播图效果的示例
2018/01/02 Javascript
JavaScript实现点击出现图片并统计点击次数功能示例
2018/07/23 Javascript
详解React项目中碰到的IE问题
2019/03/14 Javascript
vuex管理状态仓库使用详解
2020/07/29 Javascript
vue 自定指令生成uuid滚动监听达到tab表格吸顶效果的代码
2020/09/16 Javascript
electron踩坑之dialog中的callback解决
2020/10/06 Javascript
qpython3 读取安卓lastpass Cookies
2016/06/19 Python
python 列表,数组,矩阵两两转换tolist()的实例
2018/04/04 Python
django允许外部访问的实例讲解
2018/05/14 Python
对python 判断数字是否小于0的方法详解
2019/01/26 Python
Python面向对象程序设计之私有属性及私有方法示例
2019/04/08 Python
将Pytorch模型从CPU转换成GPU的实现方法
2019/08/19 Python
scrapy爬虫:scrapy.FormRequest中formdata参数详解
2020/04/30 Python
selenium学习教程之定位以及切换frame(iframe)
2021/01/04 Python
在css3中background-clip属性与background-origin属性的用法介绍
2012/11/13 HTML / CSS
Electrolux伊莱克斯巴西商店:家用电器、小家电和配件
2018/05/23 全球购物
Dockers鞋官网:Dockers Shoes
2018/11/13 全球购物
乌克兰巴士票购买网站:inBus
2021/03/12 全球购物
创先争优宣传标语
2014/10/08 职场文书
教师旷工检讨书
2015/08/15 职场文书
redis实现排行榜功能
2021/05/24 Redis
win10蓝屏0xc0000001安全模式进不了怎么办?win10出现0xc0000001的解决方法
2022/08/05 数码科技