我试图解密500MB的数据,并且即时解决大文件内存异常问题,所以解密适用于较小文件,无论如何我都可以确保我能够解密大文件,米没有得到这种内存不足的例外?用AES解密大文件
key.file的第一部分是第四部分,key.file的第二部分是关键。
我的机器有32GB的内存,所以它不是本地问题。
代码在此行中断:var so = decrTransform.TransformFinalBlock(file,0,file.Length);
私人无效DecryptData(){
X509Certificate2 cert;
var storeLocation = (StoreLocation)Enum.Parse(typeof(StoreLocation), "LocalMachine", true);
var storeName = (StoreName)Enum.Parse(typeof(StoreName), "My", true);
var findType = (X509FindType)Enum.Parse(typeof(X509FindType), "FindByThumbprint", true);
string thumbprint = Thumb;
try
{
X509Store certStore = new X509Store(storeName, storeLocation);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = certStore.Certificates.Find(findType,
thumbprint, false);
certStore.Close();
cert = new X509Certificate2(certCollection[0]);
}
catch (Exception ex)
{
throw ex;
}
RijndaelManaged alg = new RijndaelManaged();
try
{
var asd = ((RSACryptoServiceProvider)cert.PrivateKey).Decrypt("file.key.path"), true);
var iv = new byte[16];
Buffer.BlockCopy(asd, 0, iv, 0, 16);
var key = new byte[32];
Buffer.BlockCopy(asd, 16, key, 0, 32);
alg.Padding = PaddingMode.PKCS7;
alg.Mode = CipherMode.CBC;
using (ICryptoTransform decrTransform = alg.CreateDecryptor(key, iv))
{
byte[] file = ReadFile(@"encrypted.file.path");
var so = decrTransform.TransformFinalBlock(file, 0, file.Length);
File.WriteAllBytes(@"SavedData.path", so);
decrTransform.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
}
32位操作系统或64位操作系统?编译为32位.NET或64位.NET? – davidbak
整个512MB是在一个进程中加密的吗? –
事情是,一个32位.NET进程只能访问2GB(用户区域),并且可能无法分配大块连续内存。您已经为您读入的文件找到了一个〜500Mb的连续内存块。您的进程地址空间中可能没有第二个〜500Mb连续内存块用于解码数据。如果你正在运行32位。 – davidbak