2013-09-27 114 views
1

我想我DataTable保存在一個文件中,以便沒有人能夠閱讀:保存XML數據

private DataTable machineTable = new DataTable(); 
    private Rijndael crypto = Rijndael.Create(); 
    private FileStream stream; 
    ... 
    this.crypto.IV = ASCIIEncoding.ASCII.GetBytes(IV); 
    this.crypto.Key = ASCIIEncoding.ASCII.GetBytes(password); 

    this.stream = new FileStream(Global.MachineParametersDataFile, FileMode.OpenOrCreate, FileAccess.Write); 
    CryptoStream cryptoStream = new CryptoStream(this.stream, crypto.CreateEncryptor(this.crypto.Key, this.crypto.IV), CryptoStreamMode.Write); 
    this.machineTable.WriteXml(stream, XmlWriteMode.WriteSchema, true); 

密碼:32 字節IV:16字節

我的代碼產生了幾條二進制行,但其餘的完全沒有加密。

+0

你如何存儲你的IV?傳統上它會作爲前16個字節附加到密碼塊的前面。 IV不需要保密,只需要每次加密都是隨機的。另外,它看起來像是在傳遞一個字符串,你應該真的使用[GenerateIV()](http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.generateiv。 aspx)每次都得到那個隨機IV –

+0

最後,如果你想保護你的自己免受加密blob上的暴力攻擊,你應該使用[PBKDF2](http://msdn.microsoft.com/en-us/) library/system.security.cryptography.rfc2898derivebytes.aspx)來減慢密鑰生成。你可以放一個足夠大的['IterationCount'](http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.iterationcount.aspx),它需要500ms來生成密鑰,您的最終用戶只需等待500毫秒即可使用您的程序,攻擊者必須等待每次猜測密碼500毫秒(重新使用IV作爲函數的鹽) –

回答

3

您正在寫入流而不是cryptoStream。 this.machineTable.WriteXml(cryptoStream, XmlWriteMode.WriteSchema, true);