gpt4 book ai didi

python - 我可以使用 AES I.V.或随机数作为密码盐?

转载 作者:行者123 更新时间:2023-12-01 07:42:07 25 4
gpt4 key购买 nike

我正在尝试制作一个Python程序,它将获取文件 key ,然后加密文件。我已经知道 AES-GCM 和 AES-CFB 模式分别使用随机数和 IV。我目前将 IV/nonce 存储在加密文件本身中。我正在考虑是否可以使用 AES-CFB/AES-GCM 的 IV/nonce 作为我的密码哈希盐?

之前我对提供的 key 进行了哈希处理,但是当我了解 Rainbow-tables 时,我想到了使用更复杂的方法。我了解到的方法是 PBKDF2。

if filepath.endswith(EXT):
method = 'decrypt'
flag = False
with open(filepath, 'rb+') as f:
f.seek(-NONCE_SIZE,2)
iv = f.read()
os.truncate(filepath, os.path.getsize(filepath) - NONCE_SIZE)

# If the file doesn't end with the required extension,
# then identify the method as `encrypt` and do the same
# with the key provided.
else:
method = 'encrypt'
flag = True
iv = Random.new().read(NONCE_SIZE)

# Make a cipher object with the nonce and key and write
# to the file with the arguments.
# Previous approach as commented-out code line below
# key = hashlib.sha3_256(key.encode()).digest()
key = PBKDF2(key, iv, dkLen=32)
crp = getattr(AES.new(key, AES.MODE_GCM, nonce=iv), method)

我希望用作密码散列盐的 IV/nonce 能够提供所需的安全性。

最佳答案

这就是 IV 和 nonce 的用途。使用它们两次可能会对加密产生灾难性影响。根据定义,随机数是一个仅使用一次的数字。

关于python - 我可以使用 AES I.V.或随机数作为密码盐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56644870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com