我写了两个接口,一个对文件进行加密,一个是对文件进行解密,功能是实现了,但是在测试对大文件(大概在2G以上的文件)进行加密解密的时候发现这两个接口占用了几乎全部的内存,所以请高手指点,如何修改代码降低内存使用量,非常感谢!private string key = null; //默认密钥
private byte[] sKey;
private byte[] sIV;
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
private const string SERVICE_KEY_STRING = "1234abcd";/// <summary>
/// 加密文件
/// </summary>
/// <param name="filePath">输入文件路径</param>
/// <param name="savePath">加密后输出文件路径</param>
/// <param name="keyStr">加密密钥</param>
/// <returns></returns>
public bool EncryptFile(string filePath, string savePath, out string excep, string keyStr = SERVICE_KEY_STRING)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (keyStr == "")
keyStr = key;
FileStream fs = File.OpenRead(filePath);
byte[] inputByteArray = new byte[fs.Length];
fs.Read(inputByteArray, 0, (int)fs.Length);
fs.Close();
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
sKey = new byte[8];
sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
des.Key = sKey;
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close(); excep = "Encrypt File successful!";
return true;
}
catch(System.Exception Ex)
{
excep = Ex.ToString();
return false;
}
}/// <summary>
/// 解密文件
/// </summary>
/// <param name="filePath">输入文件路径</param>
/// <param name="savePath">解密后输出文件路径</param>
/// <param name="keyStr">解密密钥</param>
/// <returns></returns>
public bool DecryptFile(string filePath, string savePath, out string excep, string keyStr = SERVICE_KEY_STRING)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (keyStr == "")
keyStr = key;
FileStream fs = File.OpenRead(filePath);
byte[] inputByteArray = new byte[fs.Length];
fs.Read(inputByteArray, 0, (int)fs.Length);
fs.Close();
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
sKey = new byte[8];
sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
des.Key = sKey;
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
excep = "Decrypt file successful!";
return true;
}
catch(System.Exception Ex)
{
excep = Ex.ToString();
return false;
}
}我测试了一下,占大量内存的主要原因是因为对大文件(大概2G左右大小)读写的时候不是分块读写,而是一次性读写导致的,所以请高手指点一下,这两个接口如何修改?非常感谢!
private byte[] sKey;
private byte[] sIV;
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
private const string SERVICE_KEY_STRING = "1234abcd";/// <summary>
/// 加密文件
/// </summary>
/// <param name="filePath">输入文件路径</param>
/// <param name="savePath">加密后输出文件路径</param>
/// <param name="keyStr">加密密钥</param>
/// <returns></returns>
public bool EncryptFile(string filePath, string savePath, out string excep, string keyStr = SERVICE_KEY_STRING)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (keyStr == "")
keyStr = key;
FileStream fs = File.OpenRead(filePath);
byte[] inputByteArray = new byte[fs.Length];
fs.Read(inputByteArray, 0, (int)fs.Length);
fs.Close();
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
sKey = new byte[8];
sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
des.Key = sKey;
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close(); excep = "Encrypt File successful!";
return true;
}
catch(System.Exception Ex)
{
excep = Ex.ToString();
return false;
}
}/// <summary>
/// 解密文件
/// </summary>
/// <param name="filePath">输入文件路径</param>
/// <param name="savePath">解密后输出文件路径</param>
/// <param name="keyStr">解密密钥</param>
/// <returns></returns>
public bool DecryptFile(string filePath, string savePath, out string excep, string keyStr = SERVICE_KEY_STRING)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (keyStr == "")
keyStr = key;
FileStream fs = File.OpenRead(filePath);
byte[] inputByteArray = new byte[fs.Length];
fs.Read(inputByteArray, 0, (int)fs.Length);
fs.Close();
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
sKey = new byte[8];
sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
des.Key = sKey;
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
excep = "Decrypt file successful!";
return true;
}
catch(System.Exception Ex)
{
excep = Ex.ToString();
return false;
}
}我测试了一下,占大量内存的主要原因是因为对大文件(大概2G左右大小)读写的时候不是分块读写,而是一次性读写导致的,所以请高手指点一下,这两个接口如何修改?非常感谢!
int count = 0;
while (count<fs.Length)
{
fs.Read(inputByteArray, count, 1024);
count += 1024;
}
int count = 0;
int intread=0
while (count<fs.Length)
{
intread=fs.Read(inputByteArray, count, 1024);
count += intread;
}