【发布时间】:2019-06-18 08:31:02
【问题描述】:
我正在尝试制作一个 Python 程序,该程序将获取 file 和 key,然后它会加密文件。 我已经知道 AES-GCM 和 AES-CFB 模式分别使用随机数和 IV。我目前将 IV/nonce 存储在加密文件本身中。我正在考虑是否可以使用 AES-CFB/AES-GCM 的 IV/nonce 作为我的密码哈希盐?
之前我对提供的密钥进行哈希处理,但是当我了解 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 能够提供所需的安全性。
【问题讨论】:
-
澄清一下,密钥派生函数的使用与彩虹表攻击无关。彩虹表用于有效地查找哈希值的输入。但在这种情况下,哈希值是加密密钥。知道密码哈希的攻击者已经可以解密密文;弄清楚密码是没有意义的。 KDF 是一个故意缓慢的函数,因此暴力破解密码变得很慢。使用您的旧代码,攻击者只需执行一次 SHA 计算和一次 AES-GCM 解密即可测试密码,这非常快。
标签: python python-3.x encryption password-encryption