【问题标题】:Best way to handle changing password when used for encrypt/decrypt [closed]用于加密/解密时处理更改密码的最佳方法[关闭]
【发布时间】:2016-09-09 21:09:50
【问题描述】:

问题:

我需要加密/解密大量数据。此数据使用密码加密/解密(更具体地说,使用 RNCrytor lib)。应该能够更改此密码。

我的问题是如何才能最有效地做到这一点?

我不太好的解决方案:

除了循环遍历所有数据并对其进行解密之外,必须有更好的方法。然后使用新密码再次对其进行加密。

【问题讨论】:

    标签: swift encryption cryptography passwords change-password


    【解决方案1】:

    这是通过添加间接层解决的众多问题之一。生成一个随机密钥,使用该密钥加密数据,并将密钥存储在一个文件(或数据库列或其他任何内容)中,该文件本身使用从密码派生的密钥进行加密。

    类似的东西(注意,我不懂 Swift):

    // Generation of the data keys
    let dek = RNCryptor.randomDataOfLength(RNCryptor.FormatV3.keySize)
    let dak = RNCryptor.randomDataOfLength(RNCryptor.FormatV3.keySize)
    
    // Use these to work on the data
    let encryptor = RNCryptor.EncryptorV3(encryptionKey: dek, hmacKey: dak)
    let decryptor = RNCryptor.DecryptorV3(encryptionKey: dek, hmacKey: dak)
    
    // Save the data keys encrypted with the password
    let dek_file = RNCryptor.encryptData(dek, password: password)
    let dak_file = RNCryptor.encryptData(dek, password: password)
    // Store both dek_file and dak_file somewhere
    
    // Next time, load dek_file and dak_file from where you stored them
    let dek = RNCryptor.decryptData(dek_file, password: password)
    let dak = RNCryptor.decryptData(dek_file, password: password)
    

    【讨论】:

    • 这好多了谢谢:)
    • 更好的是,不要自己这样做:使用 GPG 或任何其他经过良好测试的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 2010-11-20
    • 2011-11-03
    • 1970-01-01
    • 2016-01-14
    • 2012-12-29
    • 1970-01-01
    • 2011-05-20
    相关资源
    最近更新 更多