Python3非对称加密算法RSA实例详解

yipeiwu_com5年前Python基础

本文实例讲述了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

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

在线RSA加密/解密工具:
http://tools.jb51.net/password/rsa_encode

文字在线加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode

MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password

在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python开发之快速搭建自动回复微信公众号功能

Python开发之快速搭建自动回复微信公众号功能

在之前的一篇文章 Python利用 AIML 和 Tornado 搭建聊天机器人微信订阅号 中用 aiml 实现了一个简单的英文聊天机器人订阅号。但是只能处理英文消息,现在用 图灵机器人...

AI人工智能 Python实现人机对话

AI人工智能 Python实现人机对话

在人工智能进展的如火如荼的今天,我们如果不尝试去接触新鲜事物,马上就要被世界淘汰啦~ 本文拟使用Python开发语言实现类似于WIndows平台的“小娜”,或者是IOS下的“Siri”。...

Python实现基于HTTP文件传输实例

本文实例讲述了Python实现基于HTTP文件传输的方法。分享给大家供大家参考。具体实现方法如下: 一、问题: 因为需要最近看了一下通过POST请求传输文件的内容 并且自己写了Serve...

深入浅析Python的类

面向对象编程时,都会遇到一个概念,类,python也有这个概念,下面我们通过代码来深入了解下。 创建和使用类 class Dog(): def __init__(self, n...

解决python报错MemoryError的问题

如下: python 32bit 最大只能使用 2G 内存,坑爹之处,超过 2G 报错MemoryError。 而 64bit python则无此限制,所以建议使用 64bit pyth...