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 相关文章推荐
Perl中著名的Schwartzian转换问题解决实现
Jun 02 Python
CentOS安装pillow报错的解决方法
Jan 27 Python
python利用sklearn包编写决策树源代码
Dec 21 Python
Python调用C++,通过Pybind11制作Python接口
Oct 16 Python
Python中利用aiohttp制作异步爬虫及简单应用
Nov 29 Python
Python创建或生成列表的操作方法
Jun 19 Python
Python进程,多进程,获取进程id,给子进程传递参数操作示例
Oct 11 Python
Django之form组件自动校验数据实现
Jan 14 Python
基于Python3.7.1无法导入Numpy的解决方式
Mar 09 Python
Django添加bootstrap框架时无法加载静态文件的解决方式
Mar 27 Python
Python Serial串口基本操作(收发数据)
Nov 06 Python
pytorch 实现L2和L1正则化regularization的操作
Mar 03 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字符串截取的简单方法
2013/07/04 PHP
thinkphp实现图片上传功能
2016/01/13 PHP
php使用timthumb生成缩略图的方法
2016/01/22 PHP
PHP常见漏洞攻击分析
2016/02/21 PHP
JavaScript高级程序设计 阅读笔记(十八) js跨平台的事件
2012/08/14 Javascript
js获得地址栏?问号后参数的方法
2013/08/08 Javascript
js实现三张图(文)片一起切换的banner焦点图
2015/08/25 Javascript
JS实现简单易用的手机端浮动窗口显示效果
2016/09/07 Javascript
jQuery简单实现列表隐藏和显示效果示例
2016/09/12 Javascript
js判断浏览器是否支持严格模式的方法
2016/10/04 Javascript
微信小程序  http请求封装详解及实例代码
2017/02/15 Javascript
微信小程序手势操作之单触摸点与多触摸点
2017/03/10 Javascript
Vue实现百度下拉提示搜索功能
2017/06/21 Javascript
Axios学习笔记之使用方法教程
2017/07/21 Javascript
webpack3之loader全解析
2017/10/26 Javascript
基于vue2.x的电商图片放大镜插件的使用
2018/01/22 Javascript
微信小程序wepy框架笔记小结
2018/08/08 Javascript
Vue源码解读之Component组件注册的实现
2018/08/24 Javascript
vue axios请求频繁时取消上一次请求的方法
2018/11/10 Javascript
一次让你了解全部JavaScript的作用域
2019/06/24 Javascript
简单说明Python中的装饰器的用法
2015/04/24 Python
Python中函数参数设置及使用的学习笔记
2016/05/03 Python
Python 实现数据库更新脚本的生成方法
2017/07/09 Python
Python去除、替换字符串空格的处理方法
2018/04/01 Python
解决Tensorflow安装成功,但在导入时报错的问题
2018/06/13 Python
Python爬虫PyQuery库基本用法入门教程
2018/08/04 Python
python numpy生成等差数列、等比数列的实例
2020/02/25 Python
关于Python3的import问题(pycharm可以运行命令行import错误)
2020/11/18 Python
Python使用Opencv实现边缘检测以及轮廓检测的实现
2020/12/31 Python
CSS3 三维变形实现立体方块特效源码
2016/12/15 HTML / CSS
美国和加拿大计算机和电子产品购物网站:TigerDirect.com
2019/09/13 全球购物
个人求职信范文分享
2014/01/06 职场文书
顶撞老师检讨书
2014/02/07 职场文书
委托书怎么写
2014/07/31 职场文书
励志演讲稿600字
2014/08/21 职场文书
pytorch 如何使用batch训练lstm网络
2021/05/28 Python