Des Decrypt read in C#


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

.cs。读取(msg, 0, msg.此行中出现长度(错误(数据错误(我的应用程序中发生了未处理的异常我不知道有什么帮助,我几乎尝试了所有东西

当基础流中有数据并希望将其解密为数组时,请使用 CryptoStreamMode.ReadRead

由于您有一个正在收集数据的空流,因此应使用 CryptoStreamMode.WriteWrite


标准注意事项适用:

  • DES是一个可怕的,破碎的算法
  • 不要使用 ASCII(或 Unicode(数据作为键
  • 不要假设加密数据可以表示为 ASCII(或 Unicode(数据
  • 切勿将加密技术添加到已发布的软件中,除非您可以解释
    • 它的作用
    • 在什么情况下会被视为过时
    • 当它过时时,你将如何成长
      • 以及当您这样做时,它不会破坏所有用户或使他们不安全。

最新更新