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连接PostgreSQL数据库的方法
Nov 28 Python
Python使用文件锁实现进程间同步功能【基于fcntl模块】
Oct 16 Python
实例详解python函数的对象、函数嵌套、名称空间和作用域
May 31 Python
Python 类属性与实例属性,类对象与实例对象用法分析
Sep 20 Python
使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解
Jan 25 Python
Python类的绑定方法和非绑定方法实例解析
Mar 04 Python
python实现贪吃蛇双人大战
Apr 18 Python
使用pytorch实现论文中的unet网络
Jun 24 Python
Python 如何实现访问者模式
Jul 28 Python
pytorch __init__、forward与__call__的用法小结
Feb 27 Python
python本地文件服务器实例教程
May 02 Python
Python多个MP4合成视频的实现方法
Jul 16 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
discuz程序的PHP加密函数原理分析
2011/08/05 PHP
PHP实现对二维数组某个键排序的方法
2016/09/14 PHP
Thinkphp事务操作实例(推荐)
2017/04/01 PHP
PHP数据库编程之MySQL优化策略概述
2017/08/16 PHP
PHP数据库操作四:mongodb用法分析
2017/08/16 PHP
PHP折半(二分)查找算法实例分析
2018/05/12 PHP
javascript下判断一个元素是否存在的代码
2010/03/05 Javascript
网页上的Javascript编辑器和代码格式化
2010/04/25 Javascript
jQuery中关于ScrollableGridPlugin.js(固定表头)插件的使用逐步解析
2014/07/17 Javascript
avascript中的自执行匿名函数应用示例
2014/09/15 Javascript
JavaScript实现带标题的图片轮播特效
2015/05/20 Javascript
如何使用jQuery技术开发ios风格的页面导航菜单
2015/07/29 Javascript
20分钟轻松创建自己的Bootstrap站点
2016/05/12 Javascript
关于网页中的无缝滚动的js代码
2016/06/09 Javascript
AngularJS ng-app 指令实例详解
2016/07/30 Javascript
AngularJS学习笔记(三)数据双向绑定的简单实例
2016/11/08 Javascript
Node.js中的require.resolve方法使用简介
2017/04/23 Javascript
微信小程序开发之animation循环动画实现的让云朵飘效果
2017/07/14 Javascript
详解Chart.js轻量级图表库的使用经验
2018/05/22 Javascript
socket io与vue-cli的结合使用的示例代码
2018/11/01 Javascript
es6中new.target的作用和使用场景简单示例分析
2020/03/14 Javascript
JS eval代码快速解密实例解析
2020/04/23 Javascript
[01:08]2014DOTA2展望TI 剑指西雅图LGD战队专访
2014/06/30 DOTA
Python入门篇之数字
2014/10/20 Python
Python脚本实现集群检测和管理功能
2015/03/06 Python
使用Python求解最大公约数的实现方法
2015/08/20 Python
flask框架蓝图和子域名配置详解
2020/01/25 Python
python 基于opencv实现图像增强
2020/12/23 Python
大专毕业生自我鉴定
2013/11/21 职场文书
小区门卫值班制度
2014/01/24 职场文书
医学专业大学生求职信
2014/07/12 职场文书
预备党员对照检查材料思想汇报
2014/09/24 职场文书
2014银行授权委托书样本
2014/10/04 职场文书
满月酒邀请函
2015/01/30 职场文书
个人党性分析总结
2015/03/05 职场文书
初三数学教学反思
2016/02/17 职场文书