简体   繁体   中英

Bad data exception in C#

i have a "bad data exception" when trying to decrypt a file using DES in C#.Any help would be great.here is my code:

namespace encrypte
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
        {
            FileStream fsInput = new FileStream(sInputFilename,FileMode.Open, FileAccess.Read);
            FileStream fsEncrypted = new FileStream(sOutputFilename,FileMode.Create,FileAccess.Write);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Mode = CipherMode.CFB;
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.Padding = PaddingMode.ISO10126;
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsEncrypted,desencrypt,CryptoStreamMode.Write);
            byte[] bytearrayinput = new byte[fsInput.Length - 1];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.FlushFinalBlock();
            fsInput.Close();
            fsEncrypted.Close();
        }
        static void DecryptFile(string sInputFilename,string sOutputFilename,string sKey)
        {
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.Mode = CipherMode.CFB;
            DES.Padding = PaddingMode.ISO10126;
            FileStream fsread = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read);
            ICryptoTransform desdecrypt = DES.CreateDecryptor();
            CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt,CryptoStreamMode.Read);
            StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
            fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
            fsDecrypted.Flush();
            fsDecrypted.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EncryptFile(@"E:\a.wmv", @"E:\b.wmv", "abcdefgh");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DecryptFile(@"E:\b.wmv", @"E:\c.wmv", "abcdefgh");
        }
    }
}

i got an "bad data" exception in this line : fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());

static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
{
    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    DES.Mode = CipherMode.CFB;
    DES.Padding = PaddingMode.ISO10126;
    FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
    ICryptoTransform desdecrypt = DES.CreateDecryptor();
    CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
    FileStream fsDecrypted = new FileStream(sOutputFilename,FileMode.Create,FileAccess.Write);
    cryptostreamDecr.CopyTo(fsDecrypted);
    fsDecrypted.Flush();
    fsDecrypted.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM