简体   繁体   中英

Encrypting datagridview values C#

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

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM