2
Pycryptodome官方的例子,我有https://www.pycryptodome.org/en/latest/src/examples.html#encrypt-data-with-rsa不清楚
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP
file_out = open("encrypted_data.bin", "wb")
recipient_key = RSA.import_key(open("receiver.pem").read())
session_key = get_random_bytes(16)
# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
file_out.write(cipher_rsa.encrypt(session_key))
# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
[ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ]
一個問題,它說我應該使用cipher.nonce
,但cipher
在例如不確定的。我應該用
cipher = AES.new(key, AES.MODE_EAX)
cipher = AES.new(key, AES.MODE_EAX, nonce)
還是別的什麼?我敢打賭cipher = AES.new(key, AES.MODE_EAX, nonce)
,但我想確保它是密碼安全的。那麼key
所需的cipher
應該是recipient_key
,對嗎?