Specified initialization vector (IV) does not match the block size for this algorithm

CODe picture CODe · Jul 10, 2011 · Viewed 9.5k times · Source
   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.

Answer

President James K. Polk picture President James K. Polk · Jul 10, 2011

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