如何加密从文本框输入的数据?

时间:2015-01-11 11:37:08

标签: c# visual-studio-2010 encryption datagridview

我不知道如何加密从Textbox输入的数据。输入的数据将显示在DataGridView中的下一个表单上。我的问题是我想在DataGridView上显示时加密数据。我在Microsoft Visual Studio 2010中使用C#。谢谢!

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

}

1 个答案:

答案 0 :(得分:1)

这是使用指定的密码短语加密和解密字符串的代码。请记住,您应该使用加密密码解密密码。

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