简体   繁体   English

我可以使用 AES IV 或随机数作为密码盐吗?

[英]Can I use the AES I.V. or nonce as a Password Salt?

I'm trying to make a Python program which will take the file and key and then it will encrypt the file.我正在尝试制作一个 Python 程序,该程序将获取文件密钥,然后对文件进行加密。 I already know that the AES-GCM and AES-CFB mode uses a nonce and IV, respectively.我已经知道 AES-GCM 和 AES-CFB 模式分别使用 nonce 和 IV。 And I currently store the IV/nonce in the encrypted file itself.我目前将 IV/nonce 存储在加密文件本身中。 I'm pondering over the idea if I can use the IV/nonce of the AES-CFB/AES-GCM as my password hashing salt?我正在思考这个想法是否可以使用 AES-CFB/AES-GCM 的 IV/nonce 作为我的密码散列盐?

Earlier I hashed the key provided, but when I came to know about Rainbow-tables, I thought of using a more sophisticated way.早些时候我对提供的密钥进行了哈希处理,但是当我了解 Rainbow-tables 时,我想到了使用更复杂的方法。 The approach I came to know about was PBKDF2.我开始知道的方法是 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)

I expect that the IV/nonce used as a password hashing salt provides the security required.我希望用作密码散列盐的 IV/nonce 提供所需的安全性。

That is what the IV and the nonce are there for already.这就是 IV 和 nonce 已经存在的原因。 Using them twice might have catastrophic effects on the encryption.使用它们两次可能会对加密产生灾难性的影响。 A nonce is by definition a number that is used only once.根据定义,随机数是只使用一次的数字。

我意识到除了创建两个不同的随机字节之外没有更明智的方法,一个用于密码派生盐,另一个用于分组密码的随机数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM