C# 从CryptoStream读取文件内容而不写入文件

C# 从CryptoStream读取文件内容而不写入文件,c#,file,encryption,cryptography,aes,C#,File,Encryption,Cryptography,Aes,写现在我正在使用的算法来加密和解密文件,现在的场景是第一个 加密文件,然后使用应用程序解密文件并读取其内容,而无需创建解密文件 我有读取文件内容的方法,但这需要一个物理路径,这应该在解密后直接从cryptostream读取 阅读Excel内容 static void ReadContent() { string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.x

写现在我正在使用的算法来加密和解密文件,现在的场景是第一个 加密文件,然后使用应用程序解密文件并读取其内容,而无需创建解密文件

我有读取文件内容的方法,但这需要一个物理路径,这应该在解密后直接从cryptostream读取

阅读Excel内容

 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);
                }
            }
        }
    }
解密

  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();
        }
    }
请帮我解决这个问题。

考虑使用文件流而不是
FileStream

使用MemoryStream fsOut=new();
int-read;
字节[]缓冲区=新字节[1048576];
使用CryptoStream csDecrypt=新加密流(fsCrypt,AES.CreateDecryptor(),CryptoStreamMode.Read);
//从解密流中读取解密的字节
//并将它们放在内存流而不是文件中。
而((read=cs.read(buffer,0,buffer.Length))>0)
{
//Application.DoEvents();
写入(缓冲区,0,读取);
}

谢谢DekuDesu