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

How to Encrypt a Data Entered from 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);

}

这里是一个用指定密码加密和解密字符串的代码。请记住,您应该使用加密的确切秘密来解密您的密码。

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