使用Aes解密“填充无效且无法删除”

时间:2017-01-30 10:55:08

标签: c# .net encryption

我正在进行Aes解密,但我不断收到异常“填充无效且无法删除”。作为我试图解密的服务器的响应给我的加密消息。这是我用来解密代码的函数

有什么想法吗?

public async static Task<string> DecriptResponse(string data)
    {
        try
        {
            byte[] decodedData = Convert.FromBase64String(data);
            using (Aes aesAlg = new AesManaged())
            {
                aesAlg.Key = Encoding.UTF8.GetBytes(Constants.DecryptionKey);
                aesAlg.IV = Encoding.UTF8.GetBytes(Constants.DecryptionIv);

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(decodedData))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            string plaintext = srDecrypt.ReadToEnd();
                            Debug.WriteLine(plaintext);
                            return plaintext;
                        }
                    }
                }

            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
            return "";
        }
        return "";
    }

0 个答案:

没有答案