2015-04-29 37 views
0

我正在使用一個winforms應用程序,讓用戶在datagridview中填寫帳戶和密碼信息,數據源是一個數據表,它將使用.net mysql連接器連接到一個mysql服務器。加密datagridview值C#

當用戶保存數據表時,數據需要加密到mysql數據庫,所以當有人攻入mysql服務器時,數據是無用的。

用於加密和解密我使用了一個名爲SimpleAES類(Simple insecure two-way "obfuscation" for C#

這對文本框和這樣的偉大工程,但我怎麼能環通一個DataGridView由用戶提供的所有值進行加密?

我嘗試了以下。

private void encryptAccounts() 
     { 
      SimpleAES simpleAES1 = new SimpleAES(); 

      string password; 
password= dataGridViewAccounts[4,0].Value.ToString(); 

      dataGridViewAccounts[4,0].Value = simpleAES1.EncryptToString(password); 
     } 

這隻會加密第一行的密碼,我怎樣才能創建一個循環的每一行

+2

你爲什麼要在數據網格中做到這一點?它不能很好地映射到數據網格的使用情況,可編輯或不可以。另外,你的加密(顯然)是可逆的 - 這真的是你想要的嗎?有人可以很容易地解密密碼,只要他們能夠訪問用於加密的密鑰 - 對於WinForms應用程序來說不是很難。 – Luaan

+0

我在數據網格中進行編輯,因爲它檢查datagridview的更改,如果有任何更新它將更新到MySQL服務器。該應用程序將僅在我們的組織內部使用,它是我們的自定義工具,用於保持數據組織,並查看用戶帳戶和密碼。 – PandaNL

+0

如何遍歷所有行和所有列? – MajkeloDev

回答

3

我怎樣才能創建一個循環的每一行

private void encryptAccounts() 
{ 
    SimpleAES simpleAES1 = new SimpleAES(); 

    // iterate over all DGV rows 
    for (int r = 0; r < dataGridViewAccounts.Rows.Count; r++) 
    { 
     if (dataGridViewAccounts[4, r].Value != null) 
     { 
      string password = dataGridViewAccounts[4, r].Value.ToString(); 
      dataGridViewAccounts[4, r].Value = simpleAES1.EncryptToString(password); 
     } 
    } 

    // OR 

    foreach (DataGridViewRow row in dataGridViewAccounts.Rows) 
    { 
     if (row.Cells[4].Value != null) 
     { 
      string password = row.Cells[4].Value.ToString(); 
      row.Cells[4].Value = simpleAES1.EncryptToString(password); 
     } 
    } 
} 
+1

請注意,您必須避免加密已加密的行,儘管:) – Luaan

+0

@Luaan,是的,這是正確的,應該有必要的驗證。我只舉例說明如何迭代網格行 – ASh

+0

當我運行代碼時,它給了我一個NullReferenceExecption未處理,我用Button_click – PandaNL