1

I don't know how to encrypt a data entered from Textbox. The data entered will appear on the next form inside a DataGridView. My problem is I want to encrypt the data when it appears on the DataGridView. I am using C# in Microsoft Visual Studio 2010. Thanks!

frmUsers f2 = new frmUsers();

private void button1_Click(object sender, EventArgs e)
{
    setGrid();
}

public void setGrid() {

    f2.dataGridView1.Columns.Add("COL1", "Firstname");
    f2.dataGridView1.Columns.Add("COL1", "Lastname");
    f2.dataGridView1.Columns.Add("COL1", "Email");
    f2.dataGridView1.Columns.Add("COL1", "Password");
    f2.Show();

    f2.dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text);

}
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
bracket17
  • 19
  • 1
  • 6

1 Answers1

1

Here is a code to encrypt and decrypt a string with specified secret phrase. Keep in mind you should decrypt your cipher with the exact secret you have encrypted it.

public static class Crypto
{
    public static string Encrypt(string text, string secret)
    {
        if (string.IsNullOrEmpty(text))
            throw new ArgumentNullException("text");
        if (string.IsNullOrEmpty(secret))
            throw new ArgumentNullException("secret");

        var salt = Encoding.UTF8.GetBytes(secret);

        using (var aes = new RijndaelManaged())
        {
            var key = new Rfc2898DeriveBytes(secret, salt);

            aes.Key = key.GetBytes(aes.KeySize / 8);

            var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

            using (var ms = new MemoryStream())
            {
                ms.Write(BitConverter.GetBytes(aes.IV.Length), 0, sizeof(int));
                ms.Write(aes.IV, 0, aes.IV.Length);
                using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                using (var sw = new StreamWriter(cs))
                {
                    sw.Write(text);
                }
                return Convert.ToBase64String(ms.ToArray());
            }
        }
    }

    public static string Decrypt(string cipher, string secret)
    {
        if (string.IsNullOrEmpty(cipher))
            throw new ArgumentNullException("cipher");
        if (string.IsNullOrEmpty(secret))
            throw new ArgumentNullException("secret");

        var salt = Encoding.UTF8.GetBytes(secret);

        using (var aes = new RijndaelManaged())
        {
            var key = new Rfc2898DeriveBytes(secret, salt);

            var bytes = Convert.FromBase64String(cipher);
            using (var ms = new MemoryStream(bytes))
            {
                aes.Key = key.GetBytes(aes.KeySize / 8);
                aes.IV = ReadByteArray(ms);

                var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
                using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                using (var sr = new StreamReader(cs))
                {
                    return sr.ReadToEnd();
                }
            }
        }
    }

    private static byte[] ReadByteArray(Stream s)
    {
        var rawLength = new byte[sizeof(int)];
        if (s.Read(rawLength, 0, rawLength.Length) != rawLength.Length)
        {
            throw new SystemException("Stream did not contain properly formatted byte array");
        }

        var buffer = new byte[BitConverter.ToInt32(rawLength, 0)];
        if (s.Read(buffer, 0, buffer.Length) != buffer.Length)
        {
            throw new SystemException("Did not read byte array properly");
        }

        return buffer;
    }
}
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74