[英]Encrypt an existing zip file
我有一個現有的zip文件,我想使用AESManaged類來加密它,但我找不到我可以在那個類中設置zip文件的密碼。 經過研究,我發現一些像'DotNetZip'這樣的圖書館可以完成任務。 但我的文件已經是.zip了,我不需要再次壓縮,我只想加密它。 任何人都可以幫助我使用AESManaged類來達到目的嗎?
謝謝
我不知道這是否是您正在尋找的,但我創建了一個加密任何文件的代碼。
這是加密器的代碼:
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上看到了類似的代碼。 所以這不是我的工作。 積分歸作者所有。
更新了基於密碼的密鑰派生(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);
}
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);
}
密鑰的字節長度在我的情況下是128位(!)= 16字節(128/8),但您可以使用Rijndael支持的任何其他長度(密鑰:128,192,256位= 16,24,32字節) 。 IV總是16個字節!
如果要在解壓縮時在原始zip文件中使用密碼,則需要重新壓縮文件並在執行此操作時添加密碼。
dotnetzip庫文檔中的這個鏈接顯示了使用該庫進行密碼加密壓縮的簡便方法。
關於安全性的補充說明
如果您完全關心加密安全性,請不要使用zip 2.0加密方法,因為它存在很大缺陷。 而是使用AES 256位加密。
下面是一些示例代碼(直接從上面的鏈接中提取),顯示使用帶有默認級別壓縮的dotnetzip庫實現AES 256位加密:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("ReadMe.txt"); // no password for this one
zip.Password= "Cool.Hand.Luke!";
zip.Encryption= EncryptionAlgorithm.WinZipAes256;
zip.AddFile("Rawdata-2008-12-18.csv");
zip.Save("Backup-AES-Encrypted.zip");
}
編輯:添加了有關原始zip文件的說明
編輯2:添加代碼
你可以使用你提到的DotNetZip(Ionic zip) ,它支持設置密碼,提供零級壓縮:
using (ZipFile zip = new ZipFile())
{
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.None;
zip.AddFile(@"MyMusic\Messiah-01.mp3");
zip.Save(ZipFileToCreate);
}
因此,只需設置密碼就沒有開銷(壓縮已經壓縮的文件)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.