C# 如果密钥错误,则解密文件包含错误数据

C# 如果密钥错误,则解密文件包含错误数据,c#,encryption,des,C#,Encryption,Des,我有在DES加密中解密文件的代码,我尝试继续获取文件,即使在密钥以不同方式解密加密文件时输入密钥。但我得到了错误 while ((data = cryptostreamDecr.ReadByte()) != -1) // Message Error : Bad Data. 我应该添加或更改哪些代码以保持进程运行解密 private static void DecryptFile(string sInputFilename, string sKey) { var DES = new

我有在DES加密中解密文件的代码,我尝试继续获取文件,即使在密钥以不同方式解密加密文件时输入密钥。但我得到了错误

 while ((data = cryptostreamDecr.ReadByte()) != -1)  // Message Error : Bad Data.
我应该添加或更改哪些代码以保持进程运行解密

private static void DecryptFile(string sInputFilename, string sKey)
{
    var DES = new DESCryptoServiceProvider();
    DES.Key = Encoding.ASCII.GetBytes(sKey);
    DES.IV = Encoding.ASCII.GetBytes(sKey);
    ICryptoTransform desdecrypt = DES.CreateDecryptor();
     using (var fsread = new FileStream(sInputFilename, FileMode.Open,
                FileAccess.ReadWrite))
            {
                using (var cryptostreamDecr = new CryptoStream(fsread,
                    desdecrypt,
                    CryptoStreamMode.Read))
                {
                    int data;

                    fsread.Flush();

                    using (var ms = new MemoryStream())
                    {
                        while ((data = cryptostreamDecr.ReadByte()) != -1)
                        {
                            ms.WriteByte((byte)data);
                        }

                        cryptostreamDecr.Close();

                        using (var fsWrite = new FileStream(sInputFilename, FileMode.Truncate))
                        {
                            ms.WriteTo(fsWrite);
                            ms.Flush();
                        }
                    }
                }
            }
        }
加密代码:

public static void EncryptFile(string sInputFilename, string sKey)
        {
            FileStream fsInput = new FileStream(sInputFilename,
               FileMode.Open,
               FileAccess.ReadWrite);

            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsInput,
               desencrypt,
               CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            fsInput.SetLength(0);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.FlushFinalBlock();
            cryptostream.Close();
            fsInput.Close();
        }
[编辑]:在百科全书中删除:

cryptostream.FlushFinalBlock()

和外接程序解密

DES.Padding=PaddingMode.None


64位是DES加密算法的唯一有效密钥大小。 8位等于一个ASCII字符意味着64位等于8个字符

如果只发送8个字符,请选中此()。它可能会解决你的问题

[编辑]
添加
DES.Padding=PaddingMode.None
解密文件中

是的,我确实使用了8个字符,我的意思是,如果我尝试在解密过程中插入密钥与加密时不同,我会得到错误:数据不正确。我没有在解密参数中使用输出目标文件,如果你的意思是问题的话?我已经解决了。但在同一行中有同样的错误。我已经发布了我的电子密码。添加
DES.Padding=PaddingMode.None
解密文件中
。它的工作。谢谢但是有新的问题。如果在加密密钥中输入相同的密钥,则解密时无法打开文件。你知道问题出在哪里吗?