我有一个小应用程序,其中读取任何文件(即.exe,.jpg,.img和所有其他文件),通过bytes[]
将其传输到File.ReadAllBytes()
,用3DES加密并保存File.WriteAllBytes()
。比有解密方法做同样的反向。我的问题是,一切都工作正常,高达300MB原始文件,比我的内存异常。有什么建议如何解决这个问题?如何处理文件> 1GB?它可以帮助使用streamreader或cryptostream读写器吗?
阅读和加密文件:
try
{
saveFileDialog1.FileName = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
fileToEncrypt = File.ReadAllBytes(openFileDialog1.FileName);
MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(textBox1.Text));
TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
TDESAlgorithm.Key = TDESKey;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.PKCS7;
ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
Results = Encryptor.TransformFinalBlock(fileToEncrypt, 0, fileToEncrypt.Length);
saveFileDialog1.FileName = openFileDialog1.SafeFileName;
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
File.WriteAllBytes(saveFileDialog1.FileName, Results);
}
TDESAlgorithm.Clear();
HashProvider.Clear();
}
}
catch
{
MessageBox.Show("Your system has not enough memory.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
解密和保存:
try
{
openFileDialog1.FileName = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
fileToDecrypt = File.ReadAllBytes(openFileDialog1.FileName);
MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(textBox1.Text));
TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
TDESAlgorithm.Key = TDESKey;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.PKCS7;
ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
Results = Decryptor.TransformFinalBlock(fileToDecrypt, 0, fileToDecrypt.Length);
saveFileDialog1.FileName = Path.GetFileName(openFileDialog1.FileName);
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
File.WriteAllBytes(saveFileDialog1.FileName, Results);
}
TDESAlgorithm.Clear();
HashProvider.Clear();
}
}
catch
{
MessageBox.Show("Master password is wrong", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
我只是爱好编码器,所以我为我的代码祈祷一些仁慈:)
textbox1.Text
是主密码。
答案 0 :(得分:0)
最后我有一些使用cryptostream的东西 - 在我的笔记本上8分钟加密2,7GB文件。 感谢Magnus的努力,我也会尝试你的代码
加密方法
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
saveFileDialog1.FileName = openFileDialog1.SafeFileName;
saveFileDialog1.ShowDialog();
FileStream readFile = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
FileStream writeFile = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write);
writeFile.SetLength(0);
byte[] storage = new byte[100];
long fileWritten = 0;
long totlen = readFile.Length;
int bytesToWrite;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(textBox1.Text));
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
TDES.Key = TDESKey;
TDES.Mode = CipherMode.ECB;
TDES.Padding = PaddingMode.PKCS7;
CryptoStream cryptStream = new CryptoStream(writeFile, TDES.CreateEncryptor(), CryptoStreamMode.Write);
while (fileWritten < totlen)
{
bytesToWrite = readFile.Read(storage, 0, 100);
cryptStream.Write(storage, 0, bytesToWrite);
fileWritten = fileWritten + bytesToWrite;
}
cryptStream.Close();
TDES.Clear();
HashProvider.Clear();
}
使用 TDES.CreateDecryptor()
代替 TDES.CreateEncryptor()
解密方法相同。
我必须说File.ReadAllBytes()
和File.WriteAllBytes()
比文件300MB大小要快得多。