Python,使用两个不同的密钥加密消息。 (Crypto.PublicKey)

时间:2019-01-28 21:19:11

标签: python rsa public-key

我想使用Python使用两个不同的密钥来加密消息。 在这种情况下,我正在使用名为Crypto.PublicKey的模块。 在下面的代码中,我只能用一个密钥进行加密,并且加密工作正常。 我尝试了许多不同的方法,但是无法使用两个不同的公共密钥来加密消息。 如果有人可以帮助我解决此问题,我将不胜感激。 非常感谢

from Crypto.PublicKey import RSA

# Creating the first public key and private key
key = RSA.generate(1024)
pubKey = key.publickey().exportKey('PEM')
privKey = key.exportKey('PEM')
key = RSA.importKey(pubKey)
key1 = RSA.importKey(privKey)

# Creating the Second public key 
key_2 = RSA.generate(1024)
pubKey_2 = key_2.publickey().exportKey('PEM')
key_2 = RSA.importKey(pubKey_2)

# Message to encrypt
msg = "Message to encrypt"

# Encrypt message with only one public key
msg_encrypted = key.encrypt(msg.encode(),32)

###########################################
# Here I would like to encrypt the message with two different public key
# thus two person will have the chance two open the file with their own private key
###########################################

# Decrypt the message with the first or the second private key
msg_decrypted = key1.decrypt(msg_encrypted)

print (msg_decrypted.decode())

0 个答案:

没有答案