国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python3非對稱加密算法RSA實例詳解

2020-02-15 23:58:06
字體:
來源:轉載
供稿:網友

本文實例講述了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.RSAimport Crypto.Randomx = 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.RSAwith 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 rsaf, 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.RSAimport Crypto.Cipher.PKCS1_v1_5import Crypto.Randomimport Crypto.Signature.PKCS1_v1_5import Crypto.Hashy = 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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新丰县| 澄江县| 贵德县| 旅游| 张家港市| 郓城县| 扬中市| 阿坝县| 漯河市| 巴林左旗| 留坝县| 桐乡市| 阿尔山市| 河津市| 漠河县| 土默特左旗| 敦化市| 永宁县| 澄城县| 曲水县| 天长市| 巧家县| 商丘市| 武川县| 大姚县| 寿光市| 静宁县| 靖远县| 屯门区| 沧州市| 金乡县| 新龙县| 乐业县| 鲁甸县| 永兴县| 如皋市| 即墨市| 泽普县| 融水| 咸丰县| 宣化县|