使用Aes加密加密和解密字符串 - C#

时间:2017-10-01 11:01:09

标签: c# arrays string encryption aes

我想将一个加密的字符串存储在SQL数据库中作为字节数组,我无法弄清楚我做错了什么。代码是这样的:

    private void loginBtn_Click(object sender, EventArgs e)
    {
        try
        {
            string password = passwordBox.Text.ToString();

            using (Aes algorithm = Aes.Create())
            {
                byte[] encryptedPassword = EncryptString(password, algorithm.Key, algorithm.IV);

                string roundTrip = DecryptString(encryptedPassword, algorithm.Key, algorithm.IV);

                MessageBox.Show("Encrypted Password: " + encryptedPassword.ToString() + '\n' + "Round Trip: " + roundTrip.ToString());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

用于'EncryptString'和'DecryptString'的代码是来自Microsoft's Aes Class Reference的代码(位于页面末尾的示例)。

我执行了我的代码,所有它在消息框中给我的是:

  

加密密码:System.Byte []

     

往返:(空地)

    static byte[] EncryptString(string str, byte[] key, byte[] IV)
    {
        if (str == null || str.Length <= 0)
            throw new ArgumentNullException("string");
        if (key == null || key.Length <= 0)
            throw new ArgumentNullException("key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");

        byte[] encrypted;

        using (Aes algorithm = Aes.Create())
        {
            algorithm.Key = key;
            algorithm.IV = IV;

            ICryptoTransform encryptor = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV);

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(str);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }

        return encrypted;
    }

    static string DecryptString(byte[] cipher, byte[] key, byte[] IV)
    {
        if (cipher == null || cipher.Length <= 0)
            throw new ArgumentNullException("cipher");
        if (key == null || key.Length <= 0)
            throw new ArgumentNullException("key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");

        string decrypted;

        using (Aes algorithm = Aes.Create())
        {
            algorithm.Key = key;
            algorithm.IV = IV;

            ICryptoTransform decryptor = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);

            using (MemoryStream msDecrypt = new MemoryStream())
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        decrypted = srDecrypt.ReadToEnd();
                    }
                }
            }
        }

        return decrypted;
    }

有人可以帮我解决吗?

P.S。文本框的密码字符设置为*

1 个答案:

答案 0 :(得分:1)

DecryptString方法中,您忘记将cipher参数作为输入传递给msDecrypt内存流的构造函数,因此方法实际上解密了空输入流,因此结果也是空的。< / p>

using (MemoryStream msDecrypt = new MemoryStream())

实际应该是:

using (MemoryStream msDecrypt = new MemoryStream(cipher))

然后一切正常。