0

I'm working on a winforms application that let the user fill in account and password information in a datagridview, the datasource is a datatable which will be connected to a mysql server using the .net mysql connector.

When the user saves the datatable the data needs to be encrypted to the mysql database, so incase someone hacks into the mysql server the data is useless.

For encrypting and decrypting I use a class called SimpleAES (Simple insecure two-way "obfuscation" for C#)

This works great for textboxes and such, but how can I loop thru a datagridview to encrypt all the values given by the user?

I tried the following.

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

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

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

This will only encrypt the password for the first row, how can I create a loop for every row

Community
  • 1
  • 1
PandaNL
  • 828
  • 5
  • 18
  • 40
  • 2
    Why would you want to do this in a data grid? It doesn't really map well to the usage of a data grid, editable or not. Also, your encryption is (obviously) reversible - is that really what you want? Someone could get decrypt the passwords rather easily if they get access to the key you used for encryption - not exactly hard for a winforms application. – Luaan Apr 29 '15 at 11:27
  • I do the editing in the datagrid because its checks the datagridview for changes and if there are any it will update it to mysql server. The application will only be used internally in our organisation, It is our custom tool to keep data organized, and view user accounts and passwords. – PandaNL Apr 29 '15 at 11:28
  • What about iterating through all rows and all columns ? – MajkeloDev Apr 29 '15 at 11:30
  • I think what you're really asking is, how to iterate through the rows of a DataGridView and get the values of specific cells, right? – waldrumpus Apr 29 '15 at 11:50

1 Answers1

3

how can I create a loop for every row

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);
        }
    }
}
ASh
  • 34,632
  • 9
  • 60
  • 82
  • 1
    Do note you have to avoid encrypting rows that are already encrypted, though :) – Luaan Apr 29 '15 at 11:44
  • @Luaan, yes, that is correct, there should be necessary validation. I only gave example how to iterate over grid Rows – ASh Apr 29 '15 at 11:46
  • When I run the code it give me a NullReferenceExecption was unhandled, Im calling theencryptAccounts with a Button_click – PandaNL Apr 29 '15 at 11:59
  • @PandaNL, `dataGridViewAccounts[4, r].Value` is null probably; check if cell value is null on each iteration – ASh Apr 29 '15 at 12:03
  • Strange, all cells are filles in the datagridviews, when using other cells like dataGridViewAccounts[2, r].Value it also stops with the nullreferenceexecption. – PandaNL Apr 29 '15 at 12:45
  • @PandaNL, check exception stack trace, it should point directly to troublesome line of code. maybe, something in `simpleAES1.EncryptToString()`? – ASh Apr 29 '15 at 12:50
  • Thank you for the directions, it was looped trhu the empty add line of the datagridview. – PandaNL Apr 29 '15 at 19:43