如何使用pycryptodomeaes在python中加密字符串?

2024-04-26 12:21:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我对密码学不熟悉,对一些代码有疑问。你知道吗

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)

file_out = open("encrypted.bin", "wb")
[ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ]

这个代码直接实现安全吗?此代码取自https://pycryptodome.readthedocs.io/en/latest/src/examples.html#encrypt-data-with-aes
如何使用生成的密钥加密字符串?


Tags: key代码fromimportdatagetbytestag