3
   public static string GenerateKey()
   {
        AesCryptoServiceProvider aesCrypto = (AesCryptoServiceProvider)AesCryptoServiceProvider.Create();

        // Use the Automatically generated key for Encryption. 
        return ASCIIEncoding.ASCII.GetString(aesCrypto.Key);
    }

    static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
    {
        FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);

        FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
        AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
        // Sets the appropriate block size for the AES Encryption Method
        AES.BlockSize = 128;
        AES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        ICryptoTransform aesencrypt = AES.CreateEncryptor();
        CryptoStream cryptostream = new CryptoStream(fsEncrypted, aesencrypt, CryptoStreamMode.Write);

        byte[] bytearrayinput = new byte[fsInput.Length];
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Close();
        fsInput.Close();
        fsEncrypted.Close();
    }

    static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
    {
        AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
        //A 64 bit key and IV is required for this provider.
        //Set secret key For DES algorithm.
        AES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        //Set initialization vector.
        AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

        //Create a file stream to read the encrypted file back.
        FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
        //Create a DES decryptor from the DES instance.
        ICryptoTransform aesdecrypt = AES.CreateDecryptor();
        //Create crypto stream set to read and do a 
        //DES decryption transform on incoming bytes.
        CryptoStream cryptostreamDecr = new CryptoStream(fsread, aesdecrypt, CryptoStreamMode.Read);
        //Print the contents of the decrypted file.
        StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
        fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
        fsDecrypted.Flush();
        fsDecrypted.Close();
    }

I'm getting the exception listed in the title at this line in the EncryptFile method.

AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

I set the BlockSize directly before that line, and I'm sure AES is suppose to use a 16 byte block size, so what do I do to get this working? I'm unsure as to why the code is still having problems with the block size.

Note: I'm simply trying some examples I found online, this isn't meant to be a lock-tight AES implementation, just something I'd like to get working so I can continue to learn about the algorithm.

Thanks for any help.

CODe
  • 2,253
  • 6
  • 36
  • 65

1 Answers1

10

The IV must be exactly the same size as the block size, which in the case of AES is 16 bytes.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • So I need to set my sKey to be exactly 16 bytes? – CODe Jul 10 '11 at 01:42
  • AES has a block size of 128 bytes, according to the [NIST](http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf) – Tim Jul 10 '11 at 01:47
  • 4
    @Tim: That's 128 bits, not bytes. 128 bits is 16 bytes. – CODe Jul 10 '11 at 02:02
  • Thanks this direct statement explaining what 16 actually meant helped me solve this problem for me. – Chris Marisic Aug 04 '11 at 20:36
  • It was not IMMEDIATELY obvious the sKey string send needs to be 16 characters long eg: @"IlikeNICEcakeTOO". Added for fresh visitors to the page. – sanepete Feb 03 '17 at 14:19