天天看點

python3 RSA加解密

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

其中 python3.6 Crypto 庫 使用 pip3 install pycryptodome 安裝

rsa 加解密的庫使用 pip3 install rsa 就行了

rsa子產品 加解密過程:

import rsa
 
 
# rsa加密
def rsaEncrypt(str):
    # 生成公鑰、私鑰
    (pubkey, privkey) = rsa.newkeys(512)
    print("公鑰:\n%s\n私鑰:\n:%s" % (pubkey, privkey))
    # 明文編碼格式
    content = str.encode("utf-8")
    # 公鑰加密
    crypto = rsa.encrypt(content, pubkey)
    return (crypto, privkey)
 
 
# rsa解密
def rsaDecrypt(str, pk):
    # 私鑰解密
    content = rsa.decrypt(str, pk)
    con = content.decode("utf-8")
    return con
 
 
if __name__ == "__main__":
 
    str, pk = rsaEncrypt("hello")
    print("加密後密文:\n%s" % str)
    content = rsaDecrypt(str, pk)
    print("解密後明文:\n%s" % content)
           

使用 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()
    # 如果私鑰有密碼 則使用相應密碼 Crypto.PublicKey.RSA.importKey(a, password)
    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)
           

使用 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)