json
json

Reputation: 29

Decrypt a file and write to same file in c#

I encrypted a file and wrote the encrypted content to the same file . But when I decrypt the file and try to write it to the same i am not able to clear the old content ie my encrypted text . How could I do that .

Encrypting code

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

Decrypting Code

static void DecryptFile(string sInputFilename,
     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.ReadWrite);

        ICryptoTransform desdecrypt = DES.CreateDecryptor();

        CryptoStream cryptostreamDecr = new CryptoStream(fsread,
           desdecrypt,
           CryptoStreamMode.Read);
        int data;

        while ((data = cryptostreamDecr.ReadByte()) != -1)
        {
            fsread.WriteByte((byte)data);
        }

        fsread.Close();
        cryptostreamDecr.Close();
    } 

Upvotes: 0

Views: 5566

Answers (1)

E.Z. Hart
E.Z. Hart

Reputation: 5757

You're trying to write to the encrypted file in place while you're decrypting it, so you end up adding extra unencrypted data to the end before you finish reading it.

Assuming for some reason that you can't just go with lcryder's suggestion, if you really need to write the data back to the same file at the end you could decrypt it in memory and write it once you're finished:

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

The memory stream holds the unencrypted data while you close the reading file stream and open a new one for writing (with FileMode.Truncate so the original contents are destroyed).

Upvotes: 1

Related Questions