Aes上的初始化向量

时间:2014-12-21 15:01:46

标签: c# encryption aes

当我加密明文时,我保存初始化向量的值(在公共变量中),当我解密密文时,传递初始化向量的值。我有一个错误"填充无效,无法删除。"如何解决这个问题?

public static string Decript(byte[] encryptedMessage, byte[] iv,byte[] privateKey) 
        {
            using (Aes aes = new AesCryptoServiceProvider())
            {
                aes.Key = privateKey;
                aes.IV = iv;
                // Decrypt the message 
                using (MemoryStream plaintext = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {                          
                        cs.Write(encryptedMessage, 0, encryptedMessage.Length);
                        //cs.FlushFinalBlock();
                        cs.Close();
                        string message = Encoding.UTF8.GetString(plaintext.ToArray());
                        return message;
                    }
                }
            }

0 个答案:

没有答案