简体   繁体   中英

Why my c# encryption is working but decryption said bad data

So im making a encryption/decryption windows forms project for fun, but my decryption app shows me a error: An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll

Additional information: Bad Data.

I can't find any fixes in the internet and in not that good in c#, so maybe you can help me.

Encryption:

static void EncryptFile(string sInputFile,
        string sOutputFile,
        string sKey)
    {
        FileStream fsInput = new FileStream(sInputFile, FileMode.Open, FileAccess.Read);
        FileStream fsEncrypted = new FileStream(sOutputFile, FileMode.Create, FileAccess.Write);

        DESCryptoServiceProvider DES = new DESCryptoServiceProvider();

        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

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

    }

Decryption:

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

How i use them:

EncryptFile(fileBox.Text, fileOutFolder+"/encrypted.txt", sSecretKey);
DecryptFile(fileBox.Text, saveFileDialog1.FileName, keyBox.Text)

The encrypted data is probably not in the target file when you read from it. You should close your streams in EncryptFile. And since stream are disposable, you should put them in a 'using' construct:

using (fsInput=[....]) 
{
    using (fsEncrypted=[..]) 
    {
         [....]
         fsEnCrypted.Close();
    }
}

I think solution to Your problem is quite simple and lies in calling DecryptFile(...) method. I don't know whole code, but try to replace SaveFileDialog object with OpenFileDialog object. I can't see any SaveFileDialog object in Your attached code, so it's only rational explanation of this strange behaviour in Your app.

Hope this will be helpfull for You.

Well... just by adding flush() and close() to my cryptoStream did work. Feel free to use this lazt as f*** encryption meth-od.

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