简体   繁体   中英

initialization vector on Aes

I save value for initialization vector (in a public variable) when i encrypt plaintext and pass that value for initialization vector when i decrypt ciphertext. I have an error "Padding is invalid and cannot be removed." how to fix that ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM