加密现有的zip文件 [英] Encrypt an existing zip file
问题描述
我有一个现有的zip文件,我想使用AESManaged类加密,但是我没有找到我可以在该类别中设置密码的位置。经过研究,我发现一些笔记本如DotNetZip可以完成任务。但是我的文件已经是一个.zip,我不需要再次压缩,我只想加密它。任何人都可以帮助我使用AESManaged类来达到目的?
谢谢
我不知道这是你正在寻找的,但是我创建了一个加密任何文件的代码。
这是加密程序的代码: / p>
private void EncryptFile(string inputFile,string outputFile)
{
string password = @yourPWhere;
UnicodeEncoding UE = new UnicodeEncoding();
byte [] key = CreateKey(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile,FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
IV = CreateIV(password_mTxtBx.Text);
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key,IV),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile,FileMode.Open);
int数据;
while((data = fsIn.ReadByte())!= -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
以下是解密器的代码:
private void DecryptFile(string inputFile,string outputFile)
{
string password = @yourPWhere;
UnicodeEncoding UE = new UnicodeEncoding();
byte [] key = CreateKey(password);
FileStream fsCrypt = new FileStream(inputFile,FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
IV = CreateIV(password_mTxtBx.Text);
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key,IV),
CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile.Remove(outputFile.Length - 4),FileMode.Create);
int数据;
while((data = cs.ReadByte())!= -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
几个月前我在codeproject上看到一个类似的代码。所以这不是我的工作。
信用转到作者。
使用基于密码的密钥派生(PBKDF2)更新:
private static int saltLengthLimit = 32;
private static byte [] GetSalt(int maximumSaltLength)
{
var salt = new byte [maximumSaltLength];
使用(var random = new RNGCryptoServiceProvider())
{
random.GetNonZeroBytes(salt);
}
返回盐;
}
public static byte [] CreateKey(string password)
{
var salt = GetSalt(10);
int iterationCount = 20000; //现在你应该使用至少10.000次迭代
(var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password,salt,iterationCount))
return rfc2898DeriveBytes.GetBytes(16);
}
创建者为IV(从密码创建):
public byte [] CreateIV(string password)
{
var salt = GetSalt(9);
const int Iterations = 325;
using(var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password,salt,Iterations))
return rfc2898DeriveBytes.GetBytes(16);
}
键的字节长度是128bit(!)= 16字节(128/8),但您可以使用Rijndael支持的任何其他长度(密钥:128,192,256位= 16,24,32字节)。
IV总是16字节!
I have an existing zip file, I want to use AESManaged class to encrypt it, but I don't find where I can set the password to the zip file in that class. After researching, I found some libaries such as 'DotNetZip' can complete the task. But my file is already a .zip, I needn't to compress again, I only want to encrypt it. Anyone can help me to use AESManaged class to ahieve the purpose?
Thanks
I don't know if this is what your are looking for but I created a code that encrypts any file.
Here's the code for the encrypter:
private void EncryptFile(string inputFile, string outputFile)
{
string password = @"yourPWhere";
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = CreateKey(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
IV = CreateIV(password_mTxtBx.Text);
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key,IV),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
Here's the code for the decrypter:
private void DecryptFile(string inputFile, string outputFile)
{
string password = @"yourPWhere";
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = CreateKey(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
IV = CreateIV(password_mTxtBx.Text);
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, IV),
CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile.Remove(outputFile.Length - 4), FileMode.Create);
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
I saw a similar code on codeproject a few months ago. So it's not directly my work. Credits go to the author.
Updated with password-based key derivation (PBKDF2):
private static int saltLengthLimit = 32;
private static byte[] GetSalt(int maximumSaltLength)
{
var salt = new byte[maximumSaltLength];
using (var random = new RNGCryptoServiceProvider())
{
random.GetNonZeroBytes(salt);
}
return salt;
}
public static byte[] CreateKey(string password)
{
var salt = GetSalt(10);
int iterationCount = 20000; // Nowadays you should use at least 10.000 iterations
using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, iterationCount))
return rfc2898DeriveBytes.GetBytes(16);
}
Creator for the IV (created from Password):
public byte[] CreateIV(string password)
{
var salt = GetSalt(9);
const int Iterations = 325;
using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, Iterations))
return rfc2898DeriveBytes.GetBytes(16);
}
The byte length of the key is in my case 128bit(!) = 16 bytes (128/8), but you can use any other length supported by Rijndael (Key: 128, 192, 256 bit = 16, 24, 32 bytes). The IV is always 16 bytes!
这篇关于加密现有的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!