我需要加密并存储上传的文件。
然后,这些文件将通过 token /票据机制进行解密和下载。
重要的是,文件不能以未加密的方式存储,系统管理员不应该能够随意访问它们。
我的问题是文件可能非常大,10 GB 大约是预期的最大文件大小。
加密过程可以根据需要持续很长时间。
但是我希望加密过程能够即时进行 - 因此没有 10 GB 的解密文件,只有内存中的一部分(我没有 10 GB 的内存)。
有什么建议可以实现这一目标吗?
最佳答案
大多数加密都是基于流的,因此这根本不应该成为问题 - 只需运行 FileStream
作为 CryptoStream
的输入即可code>,并使用 that 作为您使用数据的来源;这可能是进程内处理,也可能是目标 FileStream(在这种情况下,只需 cryptoStream.CopyTo(outputFileStream)
就足够了)。
如果我借用example from MSDN并编辑它以显示编写二进制文件:
using(FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate))
{
// Create a new Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create();
// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
using(CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateDecryptor(Key, IV),
CryptoStreamMode.Read))
using(FileStream destination = File.Create(destinationPath))
{
cStream.CopyTo(destination);
}
}
但是,cStream
可用于任何基于Stream
的读取。
关于.net 即时解密大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359128/