用于 encrypt/decrypt 时处理更改密码的最佳方法
Best way to handle changing password when used for encrypt/decrypt
问题:
我需要encrypt/decrypt很多数据。此数据是 encrypted/decrypted 使用密码(更具体地说是使用 RNCrytor lib)。应该可以更改此密码。
我的问题是如何才能最有效地做到这一点?
我不太好的解决方案:
除了遍历所有数据并解密之外,肯定有更好的方法。然后使用新密码再次加密。
这是通过添加一个间接层解决的众多问题之一。生成一个随机密钥,使用该密钥加密数据,并将该密钥存储在一个文件(或数据库列或其他)中,该文件本身使用从密码派生的密钥加密。
类似(注意,我不知道 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)
问题:
我需要encrypt/decrypt很多数据。此数据是 encrypted/decrypted 使用密码(更具体地说是使用 RNCrytor lib)。应该可以更改此密码。
我的问题是如何才能最有效地做到这一点?
我不太好的解决方案:
除了遍历所有数据并解密之外,肯定有更好的方法。然后使用新密码再次加密。
这是通过添加一个间接层解决的众多问题之一。生成一个随机密钥,使用该密钥加密数据,并将该密钥存储在一个文件(或数据库列或其他)中,该文件本身使用从密码派生的密钥加密。
类似(注意,我不知道 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)