TAHA SULTAN TEMURI
TAHA SULTAN TEMURI

Reputation: 5281

Read File Content From CryptoStream without Writing to File

Write now I am using this algorithm to encrypt and decrypt file, Now the scenario is to first encrypt file then using the application to decrypt file and read its content without creating a decrypted file.

I have the method to read content of file but this require a physical path, this should directly read from cryptostream after decryption

Read Excel Content

 static void ReadContent()
    {
        string con =
        @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;" +
        @"Extended Properties='Excel 8.0;HDR=Yes;'";
        using (OleDbConnection connection = new OleDbConnection(con))
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
            using (OleDbDataReader dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    var row1Col0 = dr[0];
                    Console.WriteLine(row1Col0);
                }
            }
        }
    }

Decryption

  static void FileDecrypt(string inputFile, string outputFile, string password)
    {
        byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
        byte[] salt = new byte[32];

        FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
        fsCrypt.Read(salt, 0, salt.Length);

        RijndaelManaged AES = new RijndaelManaged();
        AES.KeySize = 256;
        AES.BlockSize = 128;
        var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
        AES.Key = key.GetBytes(AES.KeySize / 8);
        AES.IV = key.GetBytes(AES.BlockSize / 8);
        AES.Padding = PaddingMode.PKCS7;
        AES.Mode = CipherMode.ECB;

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

        FileStream fsOut = new FileStream(outputFile, FileMode.Create);

        int read;
        byte[] buffer = new byte[1048576];

        try
        {
            while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
            {
             //   Application.DoEvents();
                fsOut.Write(buffer, 0, read);
            }
        }
        catch (CryptographicException ex_CryptographicException)
        {
            Console.WriteLine("CryptographicException error: " + ex_CryptographicException.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }

        try
        {
            cs.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error by closing CryptoStream: " + ex.Message);
        }
        finally
        {
            fsOut.Close();
            fsCrypt.Close();
        }
    }

Please help me to solve this.

Upvotes: 1

Views: 1012

Answers (1)

DekuDesu
DekuDesu

Reputation: 2292

Consider using a MemoryStream instead of FileStream

using MemoryStream fsOut = new();

int read;
byte[] buffer = new byte[1048576];

using CryptoStream csDecrypt = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);

// Read the decrypted bytes from the decrypting stream
// and place them in a memory stream instead of a file.
while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
{
    //   Application.DoEvents();
    fsOut.Write(buffer, 0, read);
}

Upvotes: 1

Related Questions