当前位置: 移动技术网 > IT编程>脚本编程>Python > Python3非对称加密算法RSA实例详解

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

2019年01月09日  | 移动技术网IT编程  | 我要评论

倭黑猩猩最臭名昭著,易经算卦方法,御龙在天换紫车坐标

本文实例讲述了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加密/解密工具:

文字在线加密解密工具(包含aes、des、rc4等):

md5在线加密工具:

在线散列/哈希算法加密工具:

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具:

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网