【发布时间】:2016-09-09 21:09:50
【问题描述】:
问题:
我需要加密/解密大量数据。此数据使用密码加密/解密(更具体地说,使用 RNCrytor lib)。应该能够更改此密码。
我的问题是如何才能最有效地做到这一点?
我不太好的解决方案:
除了循环遍历所有数据并对其进行解密之外,必须有更好的方法。然后使用新密码再次对其进行加密。
【问题讨论】:
标签: swift encryption cryptography passwords change-password
问题:
我需要加密/解密大量数据。此数据使用密码加密/解密(更具体地说,使用 RNCrytor lib)。应该能够更改此密码。
我的问题是如何才能最有效地做到这一点?
我不太好的解决方案:
除了循环遍历所有数据并对其进行解密之外,必须有更好的方法。然后使用新密码再次对其进行加密。
【问题讨论】:
标签: swift encryption cryptography passwords change-password
这是通过添加间接层解决的众多问题之一。生成一个随机密钥,使用该密钥加密数据,并将密钥存储在一个文件(或数据库列或其他任何内容)中,该文件本身使用从密码派生的密钥进行加密。
类似的东西(注意,我不懂 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)
【讨论】: