我尝试在python中使用AES-GCM加密进行文件加密。我使用的是Cryptodome
包,我知道如何在基于this example的单个纯文本字符串上使用encrypt_and_digest
和{
加密
def encrypt(self, filename):
iv = get_random_bytes(16)
cipher = AES.new(self.K4, AES.MODE_GCM, iv)
try:
with open(filename, 'rb') as src:
with open(filename + ".enc", 'wb') as des:
des.write(iv)
for block in iter(lambda: src.read(AES.block_size * 128), b''):
if len(block) == AES.block_size * 128:
des.write(cipher.encrypt(block))
# Padding the last block
else:
remain = len(block) % 16
if remain == 0:
remain = 16
block += utility.to_bytes((chr(remain) * remain))
des.write(cipher.encrypt(block))
except (IOError, OSError):
raise IOError("Cannot open the file to encrypt")
解密
^{pr2}$正如您可以清楚地看到的,没有执行身份验证。有什么想法吗?在
目前没有回答
相关问题 更多 >
编程相关推荐