加密现有的zip文件 [英] Encrypt an existing zip file
问题描述
我有一个现有的压缩文件,我想用AESManaged级加密,但我不觉得,我可以将密码设置为在类中的zip文件。经过研究,我发现了一些libaries如'DotNetZip就可以完成任务。但我的文件已经是一个.zip,我不需要再压缩,我只想加密。任何人都可以帮我用AESManaged类ahieve的目的是什么?
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?
感谢
推荐答案
我不知道如果这是你要找的,但我创建了一个加密的文件中的代码
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();
}
下面是该解密的代码:
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();
}
我看到CodeProject上的类似的代码在几个月前。因此,它不是直接我的工作。
学分转到笔者
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:
public static byte[] CreateKey(string password)
{
var salt = new byte[] { 1, 2, 23, 234, 37, 48, 134, 63, 248, 4 };
const int Iterations = 9872;
using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, Iterations))
return rfc2898DeriveBytes.GetBytes(16);
}
造物主为IV(从密码创建):
Creator for the IV (created from Password):
public byte[] CreateIV(string password)
{
var salt = new byte[] { 4, 7, 21, 199, 45, 63, 138, 12, 213, 1 };
const int Iterations = 325;
using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, Iterations))
return rfc2898DeriveBytes.GetBytes(16);
}
更改盐和迭代参数,其他随机数字。
Change the salt and iteration parameters to other random numbers.
密钥的字节长度是在我的情况下,128位= 16个字节(八分之一百二十八),但你可以使用Rijndael算法(重点支持的任何其他长度(!):128,192, 256位= 16,24,32字节)。
中的IV始终是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屋!