2016-05-13 36 views
1

問題:最佳方式/解密

我需要加密/解密大量的數據。該數據使用密碼進行加密/解密(更具體地說,使用RNCrytor庫)。一個應該能夠改變這個密碼。

我的問題是如何最有效地做到這一點?

我沒有那麼偉大的解決方案:

必須有不是通過所有的數據循環和解密,其它更好的方法。然後使用新密碼再次加密。

回答

5

這是通過添加一層間接尋址解決的許多問題之一。生成一個隨機密鑰,使用該密鑰加密數據,並將密鑰存儲在文件(或數據庫列或其他)中,該文件本身使用從密碼派生的密鑰加密。

喜歡的東西(注意,我不知道斯威夫特):

// 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) 
+0

這是好多了謝謝:) – Mat0

+0

更重要的是,不這樣做自己:使用GPG或任何其他行之有效的解決方案。 –