James James - 1 month ago 10
C# Question

Encrypt an existing zip file

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

Answer

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!

Comments