C# 正在尝试解密字符串,收到错误:填充无效且无法删除?我做错了什么?

C# 正在尝试解密字符串,收到错误:填充无效且无法删除?我做错了什么?,c#,encryption,C#,Encryption,嗨,我尝试了一些不同的选择,但我似乎无法纠正这个问题。 这是我的解密代码 public string DecryptStringAES(string cipherText, int AccountID = 0) { string sharedSecret = ""; AccountID = (AccountID > 0) ? AccountID : SessionVars.Current.varAccountID; sharedSe

嗨,我尝试了一些不同的选择,但我似乎无法纠正这个问题。 这是我的解密代码

public string DecryptStringAES(string cipherText, int AccountID = 0)
    {
        string sharedSecret = "";

        AccountID = (AccountID > 0) ? AccountID : SessionVars.Current.varAccountID;
        sharedSecret = "#c3x%" + AccountID + "n^/]R";

        if (string.IsNullOrEmpty(cipherText))
            throw new ArgumentNullException("cipherText");
        if (string.IsNullOrEmpty(sharedSecret))
            throw new ArgumentNullException("sharedSecret");

        // Declare the RijndaelManaged object
        // used to decrypt the data.
        RijndaelManaged aesAlg = null;

        // Declare the string used to hold
        // the decrypted text.
        string plaintext = null;

        try
        {
            // generate the key from the shared secret and the salt
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

            // Create the streams used for decryption. 
               string dummyData = cipherText.Trim().Replace(" ", "+");
               if (dummyData.Length % 4 > 0)
                   dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
            byte[] bytes = Convert.FromBase64String(dummyData);
            using (MemoryStream msDecrypt = new MemoryStream(bytes))
            {
                // Create a RijndaelManaged object
                // with the specified key and IV.
                aesAlg = new RijndaelManaged();
                aesAlg.BlockSize = 128;
                aesAlg.KeySize = 256;
                aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
                aesAlg.Padding = PaddingMode.PKCS7;
                // Get the initialization vector from the encrypted stream
                aesAlg.IV = ReadByteArray(msDecrypt);
                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                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.

                        plaintext = srDecrypt.ReadToEnd();


                }
            }
        }
        finally
        {
            // Clear the RijndaelManaged object.
            if (aesAlg != null)
                aesAlg.Clear();
        }

        return plaintext;
    }

    private static byte[] ReadByteArray(Stream s)
    {
        byte[] 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");
        }

        byte[] 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;
    }

完整的错误消息是什么?它会给你一个行号吗?嗨,Adam,行号是“明文=srDecrypt.ReadToEnd();”,错误消息是“加密异常未被用户代码处理”。“填充无效,无法删除……如有任何帮助,将不胜感激。