python3 可以使用 Crypto.PublicKey.RSA 和 rsa 生成公钥、私钥。

python3 可以使用 Crypto.PublicKey.RSA 和 rsa 生成公钥、私钥。

其中 python3.6 Crypto 库的安装方式请参考连接:https://blog.csdn.net/qq_42486920/article/details/80850974

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 生成公钥、私钥:

  1. import Crypto.PublicKey.RSA
  2. import Crypto.Random
  3. x = Crypto.PublicKey.RSA.generate(2048)
  4. a = x.exportKey("PEM") # 生成私钥
  5. b = x.publickey().exportKey() # 生成公钥
  6. with open("a.pem", "wb") as x:
  7. x.write(a)
  8. with open("b.pem", "wb") as x:
  9. x.write(b)
  10. y = Crypto.PublicKey.RSA.generate(2048, Crypto.Random.new().read) # 使用 Crypto.Random.new().read 伪随机数生成器
  11. c = y.exportKey() # 生成私钥
  12. d = y.publickey().exportKey() #生成公钥
  13. with open("c.pem", "wb") as x:
  14. x.write(c)
  15. with open("d.pem", "wb") as x:
  16. x.write(d)


使用 Crypto.PublicKey.RSA.importKey(private_key) 生成公钥和证书:

  1. import Crypto.PublicKey.RSA
  2. with open("a.pem", "rb") as x:
  3.     xx = Crypto.PublicKey.RSA.importKey(x.read())
  4. b = xx.publickey().exportKey()   # 生成公钥
  5. with open("b.pem", "wb") as x:
  6.     x.write(b)
  7.     
  8. a = xx.exportKey("DER")   # 生成 DER 格式的证书
  9. with open("a.der", "wb") as x:
  10.     x.write(a)

使用 rsa 生成公钥、私钥:

  1. import rsa
  2. f, e = rsa.newkeys(2048) # 生成公钥、私钥
  3. e = e.save_pkcs1() # 保存为 .pem 格式
  4. with open("e.pem", "wb") as x: # 保存私钥
  5. x.write(e)
  6. f = f.save_pkcs1() # 保存为 .pem 格式
  7. with open("f.pem", "wb") as x: # 保存公钥
  8. x.write(f)


RSA非对称加密算法实现:

使用Crypto模块:

  1. import Crypto.PublicKey.RSA
  2. import Crypto.Cipher.PKCS1_v1_5
  3. import Crypto.Random
  4. import Crypto.Signature.PKCS1_v1_5
  5. import Crypto.Hash
  6. y = b"abcdefg1234567"
  7. with open("b.pem", "rb") as x:
  8.     b = x.read()
  9.     cipher_public = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(b))
  10.     cipher_text = cipher_public.encrypt(y) # 使用公钥进行加密
  11. with open("a.pem", "rb") as x:
  12.     a = x.read()
  13.     cipher_private = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(a))
  14.     text = cipher_private.decrypt(cipher_text, Crypto.Random.new().read)    # 使用私钥进行解密
  15. assert text == y    # 断言验证
  16. with open("c.pem", "rb") as x:
  17.     c = x.read()
  18.     c_rsa = Crypto.PublicKey.RSA.importKey(c)
  19.     signer = Crypto.Signature.PKCS1_v1_5.new(c_rsa)
  20.     msg_hash = Crypto.Hash.SHA256.new()
  21.     msg_hash.update(y)
  22.     sign = signer.sign(msg_hash)    # 使用私钥进行'sha256'签名
  23. with open("d.pem", "rb") as x:
  24.     d = x.read()
  25.     d_rsa = Crypto.PublicKey.RSA.importKey(d)
  26.     verifer = Crypto.Signature.PKCS1_v1_5.new(d_rsa)
  27.     msg_hash = Crypto.Hash.SHA256.new()
  28.     msg_hash.update(y)
  29.     verify = verifer.verify(msg_hash, sign) # 使用公钥验证签名
  30.     print(verify)

运行结果:

True


使用 rsa 模块:

  1. import rsa
  2. y = b"abcdefg1234567"
  3. with open("e.pem", "rb") as x:
  4.     e = x.read()
  5.     e = rsa.PrivateKey.load_pkcs1(e)    # load 私钥
  6. with open("f.pem", "rb") as x:
  7.     f = x.read()
  8.     f = rsa.PublicKey.load_pkcs1(f) # load 公钥,由于之前生成的私钥缺少'RSA'字段,故无法 load
  9. cipher_text = rsa.encrypt(y, f) # 使用公钥加密
  10. text = rsa.decrypt(cipher_text, e)   # 使用私钥解密
  11. assert text == y    # 断言验证
  12. sign = rsa.sign(y, e, "SHA-256") # 使用私钥进行'sha256'签名
  13. verify = rsa.verify(y, sign, f)  # 使用公钥验证签名
  14. print(verify)

运行结果:

True
上一篇:git@gitee.com: Permission denied (publickey).报错解决


下一篇:Permission denied (publickey). 解决方法