-1

while encrypting an audio file using C#, i got an exception that "Specified inetialisation vector(IV) does not match the block size of the algorithm". and i'm using Rijndael algorithm provided by the cryptography class. what i'm supposed to do to solve this exception? my code is given below:

      public void EncryptFile(string inputFile, string outputFile)
      {

        try
        {
            inputFile = textBox_path.Text;
            String password = "keykey";

            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);
            string cryptFile = outputFile;
            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);


            RijndaelManaged RMCrypto = new RijndaelManaged();
            CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), 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();
            MessageBox.Show("encryption is completed!!");
        }
        catch(Exception e)
        {
            MessageBox.Show(e.Message);

        }

  }

and my function call is:

  • 1
    What are you supposed to do? Well, based on the error I'd say you need to have the IV match the block size. Without seeing your code or more information that's about the best answer you'll get, unless someone here can read minds.... – Tim May 10 '14 at 07:43
  • Please post your code. – ᗩИᎠЯƎᗩ May 10 '14 at 07:54
  • possible duplicate of [Specified initialization vector (IV) does not match the block size for this algorithm](http://stackoverflow.com/questions/944833/specified-initialization-vector-iv-does-not-match-the-block-size-for-this-algo) – Simon MᶜKenzie May 10 '14 at 08:58

1 Answers1

0

There is a similar question here.

With Rijndael, you can choose the block sizes 128, 160, 192, 224, or 256 bits. Then you must choose an initialization vector of the same length:

                using (RijndaelManaged rm = new RijndaelManaged())
                {
                    rm.BlockSize = 128;
                    Rfc2898DeriveBytes keyDerivator = new Rfc2898DeriveBytes(password, salt, KeyGenIterationCount); //derive key and IV from password and salt using the PBKDF2 algorithm
                    rm.IV = keyDerivator.GetBytes(16); //16 bytes (128 bits, same as the block size)
                    rm.Key = keyDerivator.GetBytes(32);

                    //(encrypt here)
                }

In any case, I would recommend using the AesCryptoServiceProvider class instead, since it's FIPS-compilant. Read more about the differences here: http://blogs.msdn.com/b/shawnfa/archive/2006/10/09/the-differences-between-rijndael-and-aes.aspx

Community
  • 1
  • 1