C# 当用C解密AES时,要解密的数据长度是无效消息#

C# 当用C解密AES时,要解密的数据长度是无效消息#,c#,aes,C#,Aes,我有两个按钮使用AES算法,一个用于加密,另一个用于解密。当我应用加密时可以,但当我应用解密时,我会遇到此消息错误 我正在使用Convert.FromBase64String,但它仍然不起作用 private void Encryption_Click(object sender, EventArgs e) {SaveFileDialog save = new SaveFileDialog(); save.FileName = "DefaultOutp

我有两个按钮使用AES算法,一个用于加密,另一个用于解密。当我应用加密时可以,但当我应用解密时,我会遇到此消息错误

我正在使用
Convert.FromBase64String
,但它仍然不起作用

  private void Encryption_Click(object sender, EventArgs e)
        {SaveFileDialog save = new SaveFileDialog();
            save.FileName = "DefaultOutputName.txt"
            save.Filter = "Text File | *.txt";

            if (save.ShowDialog() == DialogResult.OK)

            {

                StreamWriter writer = new StreamWriter(save.OpenFile());

                cipherdata = textBox2.Text;

                plainbytes = Encoding.ASCII.GetBytes(cipherdata);
                plainkey= Encoding.ASCII.GetBytes("0123456789abcdef");
                desobj.Key = plainkey;
                desobj.Mode = CipherMode.CBC;
                desobj.Padding = PaddingMode.PKCS7;
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(ms, desobj.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(plainbytes, 0, plainbytes.Length);
                cs.Close();
                cipherbytes = ms.ToArray();
                Convert.ToBase64String(cipherbytes);
                MessageBox.Show(cipherbytes.Length.ToString());
                ms.Close();
                writer.WriteLine(Encoding.ASCII.GetString(cipherbytes));

                writer.Dispose();

                writer.Close();

            }
        }

        private void decryption_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string s = File.ReadAllText(ofd.FileName);

                cipherbytes = Encoding.ASCII.GetBytes(s); 
                System.IO.MemoryStream ms1 = new System.IO.MemoryStream(cipherbytes);
                CryptoStream cs1 = new CryptoStream(ms1, desobj.CreateDecryptor(), CryptoStreamMode.Read);
                cs1.Read(cipherbytes, 0, cipherbytes.Length);
                plainbytes2 = ms1.ToArray();

                cs1.Close();
                ms1.Close();
                textBox2.Text = Encoding.ASCII.GetString(plainbytes2);
            }
        }

请学习如何使用堆栈溢出,并阅读如何提高问题的质量。然后,你的问题包括你的完整源代码,你有作为一个,它可以被编译和测试的其他人。请参阅:您应该检查输出文件,看看是否有任何数据写入其中。您还应该处理那些
IDisposable
对象保存加密文本及其工作的i check文件。在关闭加密流之前,必须刷新加密流的finalBlock()。另外,尝试使用Using{}块来确保流先被关闭,然后被释放,而不是被释放,然后被关闭。祝你好运