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-3-print重定向输出的几种方法总结
May 11 Python
python生成ppt的方法
Jun 07 Python
Python 比较文本相似性的方法(difflib,Levenshtein)
Oct 15 Python
python爬虫神器Pyppeteer入门及使用
Jul 13 Python
python实现字典嵌套列表取值
Dec 16 Python
django 实现简单的插入视频
Apr 07 Python
通过python调用adb命令对App进行性能测试方式
Apr 23 Python
keras自动编码器实现系列之卷积自动编码器操作
Jul 03 Python
python 模拟登陆163邮箱
Dec 15 Python
PyQt5中QSpinBox计数器的实现
Jan 18 Python
python 指定源路径来解决import问题的操作
Mar 04 Python
numpy数据类型dtype转换实现
Apr 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的FTP学习(三)
2006/10/09 PHP
一些PHP写的小东西
2006/12/06 PHP
PHP daddslashes 使用方法介绍
2012/10/26 PHP
2014年10个最佳的PHP图像操作库
2014/07/14 PHP
PHP中session跨子域的三种实现方法
2016/07/25 PHP
php实现文章置顶功能的方法
2016/10/20 PHP
php实现银联商务公众号+服务窗支付的示例代码
2019/10/12 PHP
jQuery 1.5最新版本的改进细节分析
2011/01/19 Javascript
js 分页全选或反选标识实现代码
2011/08/09 Javascript
jQuery学习笔记(4)--Jquery中获取table中某列值的具体思路
2013/04/10 Javascript
JavaScript自定义方法实现trim()、Ltrim()、Rtrim()的功能
2013/11/03 Javascript
window.location.href IE下跳转失效的解决方法
2014/03/27 Javascript
js统计页面的来访次数实现代码
2014/05/09 Javascript
nodejs调用cmd命令实现复制目录
2015/05/04 NodeJs
详解JavaScript中的客户端消息框架设计原理
2015/06/24 Javascript
jQuery获取某天的农历日期并判断是否除夕或新年的方法
2016/03/01 Javascript
无需 Flash 使用 jQuery 复制文字到剪贴板
2016/04/26 Javascript
深入理解JavaScript创建对象的多种方式以及优缺点
2017/06/01 Javascript
基于bootstrop常用类总结(推荐)
2017/09/11 Javascript
node.js读取Excel数据(下载图片)的方法示例
2018/08/02 Javascript
微信小程序中使用 async/await的方法实例分析
2020/05/06 Javascript
[06:25]DOTA2英雄梦之声_第17期_大地之灵
2014/06/20 DOTA
用Python的Tornado框架结合memcached页面改善博客性能
2015/04/24 Python
Python字符和字符值(ASCII或Unicode码值)转换方法
2015/05/21 Python
在Python中操作字典之update()方法的使用
2015/05/22 Python
python: 自动安装缺失库文件的方法
2018/10/22 Python
对Python的zip函数妙用,旋转矩阵详解
2018/12/13 Python
Django将默认的SQLite更换为MySQL的实现
2019/11/18 Python
pymysql模块使用简介与示例
2020/11/17 Python
CSS3 制作旋转的大风车(充满童年回忆)
2013/01/30 HTML / CSS
Ajax的工作原理
2015/12/04 面试题
经管应届生求职信
2013/11/17 职场文书
初三学生评语大全
2014/04/24 职场文书
党员个人查摆剖析材料
2014/10/16 职场文书
检讨书大全
2015/01/27 职场文书
springboot读取nacos配置文件
2022/05/20 Java/Android