1
 public Stream DecryptFile(string inputFile)//, string outputFile)
    {
        {  
                string password = @"mykey"; // Your Key Here

                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

                RijndaelManaged RMCrypto = new RijndaelManaged();

                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateDecryptor(key, key),
                    CryptoStreamMode.Read);


                StreamReader sr = new StreamReader(cs);



              Stream s = sr.BaseStream;
                //sr.Close();
                //fsCrypt.Close();
             return s;
        }
    }

In this code there is a problem that stream is not closing properly. If I close it before returning the value then it throws an error.

DDave
  • 608
  • 6
  • 17
Anoop Mishra
  • 1,005
  • 2
  • 10
  • 33

3 Answers3

3

fsCrypt.Close(); should be performed, but sr.Close(); should not be performed, since the caller of your function should be able to use the Stream.

Also, in order to properly close streams when errors occur, use a disposable context:

using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
{
    RijndaelManaged RMCrypto = new RijndaelManaged();

    CryptoStream cs = new CryptoStream(fsCrypt,
        RMCrypto.CreateDecryptor(key, key),
        CryptoStreamMode.Read);

    StreamReader sr = new StreamReader(cs);
    Stream s = sr.BaseStream;
    return s;
}

The caller should also use this pattern:

using (var stream = DecryptFile(string inputFile))
{
    // do something with decrypted file
}
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • if you say things like "disposable context", explain it because new programmers in the higher languages doesn´t know what actually happens behind the scenes or why they don´t need to go for the resque to the stack and the heap anymore . Usings are not the easiest concept before you have a few years in the field – Mikael Puusaari May 25 '16 at 05:41
  • But one more issue when i also close in decrypt function as you described above it show can not read from a closed file. – Anoop Mishra May 25 '16 at 06:06
1
using (FileStream fs = new FileStream(filePath, FileMode.Open,     FileAccess.Read, FileShare.ReadWrite))
{

//code here
}

I think it was introduced in ,NET 3.0 or so, and you don´t need to close streams anymore

Everything inside the using brackets will be automatically closed and be disposed of when the code leaves that part

Mikael Puusaari
  • 854
  • 1
  • 10
  • 14
1

Its propably much better to realize it with usings. Usings close and dispose the underlying stream for you.

public Stream DecryptFile(string inputFile)//, string outputFile)
{
    string password = @"mykey"; // Your Key Here
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(password);
    using(var fsCrypt = new FileStream(inputFile, FileMode.Open)
    {
        RijndaelManaged RMCrypto = new RijndaelManaged();
        using(CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read))
        {
            StreamReader sr = new StreamReader(cs);
            Stream s = sr.BaseStream;
            return s;
        }
    }
}
Marius
  • 562
  • 5
  • 26