Search code examples
c#encryptionencryption-symmetric

Password as key for AES Encryption/Decryption


I'm working on a project where I have to encrypt and decrypt chosen files by user. How can I use a password from user as a key for AES encryption/decryption? Right now they can enter passwords of 8 or 16 characters long. I don't want to force the user to specify a password of 8 or 16 characters.

public static void EncryptFile(string file, string password)
{
    try
    {
        string outputFile = Path.GetFileNameWithoutExtension(file) + "-encrypted" + Path.GetExtension(file);
        byte[] fileContent = File.ReadAllBytes(file);
        UnicodeEncoding UE = new UnicodeEncoding();

        using (AesCryptoServiceProvider AES = new AesCryptoServiceProvider())
        {
            AES.Key = UE.GetBytes(password);
            AES.IV = new byte[16];
            AES.Mode = CipherMode.CBC;
            AES.Padding = PaddingMode.PKCS7;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                CryptoStream cryptoStream = new CryptoStream(memoryStream, AES.CreateEncryptor(), CryptoStreamMode.Write);

                cryptoStream.Write(fileContent, 0, fileContent.Length);
                cryptoStream.FlushFinalBlock();

                File.WriteAllBytes(outputFile, memoryStream.ToArray());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception thrown while encrypting the file!" + "\n" + ex.Message);
    }
}

Solution

  • AES in .net uses by default a 256 bit key and a 128 bit IV .

    SHA256 and MD5 hash algorithms create a 256 bit and a 128 bit hash respectively.

    Hmmm.

    byte[] passwordBytes = UE.GetBytes(password);
    byte[] aesKey = SHA256Managed.Create().ComputeHash(passwordBytes);
    byte[] aesIV = MD5.Create().ComputeHash(passwordBytes);
    AES.Key = aesKey;
    AES.IV = aesIV;
    AES.Mode = CipherMode.CBC;
    AES.Padding = PaddingMode.PKCS7;