none
Encrypt and decrypt records with button RRS feed

  • Question

  • Hello,

    I'm using Visual Studio 2013. I'm trying to create a button that when clicked encrypts the data in a database as I'm exporting it and save it to a file and provide a decrypt key for the decryption part. 

    This is my code for the export button

     private void export_btn_Click(object sender, EventArgs e)
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
                worksheet = workbook.Sheets["Sheet1"];
                worksheet = workbook.ActiveSheet;
                worksheet.Name = "PatientDetails";

                for (int i = 1; i < dataGridView1.Columns.Count+1; i++)
                {
                    worksheet.Cells[1,i] = dataGridView1.Columns[i - 1].HeaderText;
                }

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    }
                }

                var saveFileDialoge = new SaveFileDialog();
                saveFileDialoge.FileName = "output";
                saveFileDialoge.DefaultExt = ".xlsx";
                if (saveFileDialoge.ShowDialog()==DialogResult.OK)
                {
                    workbook.SaveAs(saveFileDialoge.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                }
                app.Quit();
            }

    Any suggestions where I can put encryption algorithms (preferably AES/DES or paillier if possible)?

    Thanks

    Monday, August 27, 2018 11:04 AM

All replies

  • Where you put the encryption? That would need to happen as you write the data to disk. In your case it seems like that would be inside your nested for loop where you're setting the cell values. You can use an algorithm to take the string and encrypt it before saving it to the cell.

    But I question the benefit in doing this. If someone looks at the file then they'll see the rows and columns but just binary data. I think it would be better to save the file to disk and then encrypt the file itself. It would also be faster as you can then decrypt the file and read it instead of having to decrypt each cell.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, August 27, 2018 2:46 PM
    Moderator
  • Well, AES/DES is symmetric, thus not really useful.

    What's your concrete use-case? What about zipping it with a password?

    Monday, August 27, 2018 2:57 PM
  • Hi Zeme123,

    According to your description, I am sure what is your question, if you want to Encrypt the records, please refer to the following AesClass.

     public class AesClass
        {
    
    /// <summary>
            ///  AES Encrypt
            /// </summary>
            /// <param name="str">contect</param>
            /// <param name="key">Encrypt key</param>
            /// <returns></returns>
            public static string AesEncrypt(string str, string key)
            {
                if (string.IsNullOrEmpty(str)) return null;
                Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
    
                RijndaelManaged rm = new RijndaelManaged
                {
                    Key = Encoding.UTF8.GetBytes(key),
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };
    
                ICryptoTransform cTransform = rm.CreateEncryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            /// <summary>
            ///  AES Decrypt
            /// </summary>
            /// <param name="str">contect</param>
            /// <param name="key">Encrypt key</param>
            /// <returns></returns>
            public static string AesDecrypt(string str, string key)
            {
                if (string.IsNullOrEmpty(str)) return null;
                Byte[] toEncryptArray = Convert.FromBase64String(str);
    
                RijndaelManaged rm = new RijndaelManaged
                {
                    Key = Encoding.UTF8.GetBytes(key),
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };
    
                ICryptoTransform cTransform = rm.CreateDecryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                return Encoding.UTF8.GetString(resultArray);
            }
        }


    #Usage:

    string key = "Test";
     for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = AesClass.AesEncrypt( dataGridView1.Rows[i].Cells[j].Value.ToString(),key);
                    }
                }



    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 28, 2018 6:15 AM
    Moderator
  • This encrypts cells, not records..

    Tuesday, August 28, 2018 8:32 AM
  • You're back again ;)
    Tuesday, August 28, 2018 9:28 AM
  • As this was a two part answer:

    1) I don't think using AES/DES as a symmetric encryption is useful.

    2) If symmetric encryption is useful in the OP's use-case, the question was: Why not using a password protected zip.

    Tuesday, August 28, 2018 9:30 AM