C# Des在C中解密读取#

C# Des在C中解密读取#,c#,encryption,des,C#,Encryption,Des,此行出现cs.Read(msg,0,msg.Length)错误(错误数据) 我的应用程序中发生了未处理的异常 我不知道有什么需要帮助的,我已经尝试了几乎所有的方法当您在底层流中有数据并且想要将其解密为数组时,请使用CryptoStreamMode.Read和Read 因为您有一个正在收集数据的空流,所以应该使用CryptoStreamMode.Write和Write 标准警告适用于: DES是一个糟糕的、失败的算法 不要使用ASCII(或Unicode)数据作为键 不要假设加密数据可以表示为

此行出现cs.Read(msg,0,msg.Length)错误(错误数据) 我的应用程序中发生了未处理的异常
我不知道有什么需要帮助的,我已经尝试了几乎所有的方法

当您在底层流中有数据并且想要将其解密为数组时,请使用
CryptoStreamMode.Read
Read

因为您有一个正在收集数据的空流,所以应该使用
CryptoStreamMode.Write
Write


标准警告适用于:

  • DES是一个糟糕的、失败的算法
  • 不要使用ASCII(或Unicode)数据作为键
  • 不要假设加密数据可以表示为ASCII(或Unicode)数据
  • 除非您可以解释,否则不要在发布的软件中添加加密
    • 它做什么
    • 在什么情况下它将被视为过时
    • 当它过时时,你将如何让它长大
      • 当你这么做的时候,它将不会破坏你所有的用户,或者让他们不安全
static byte[] desdecrypt(Mode mode, byte[] IV, byte[] key, byte[] msg)
    {
        using (var des = new DESCryptoServiceProvider())
        {
            des.IV = IV;
            des.Key = key;
            des.Mode = CipherMode.CBC;
            des.Padding = PaddingMode.PKCS7;

            using (var mstream = new MemoryStream())
            {
                CryptoStream cs = null;
                if (mode == Mode.DECRYPT)
                {
                    cs = new CryptoStream(mstream, des.CreateDecryptor(), CryptoStreamMode.Read);
                }
                if (cs == null)
                    return null;

                cs.Read(msg, 0, msg.Length); 
                return mstream.ToArray();
            }
        }
        return null;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        string a = textBox4.Text;
        string ab = textBox6.Text;
        byte[] IV = Encoding.ASCII.GetBytes(ab);
        string aa = textBox7.Text;
        byte[] key = Encoding.ASCII.GetBytes(aa);
        byte[] decrypted = desdecrypt(Mode.DECRYPT, IV, key, Encoding.ASCII.GetBytes(a));
        textBox5.Text = Encoding.ASCII.GetString(decrypted);
    }