C# – “Specified initialization vector (IV) does not match the block size for this algorithm” using an CryptoStream

.netc++cryptostreamrijndaelmanaged

I've had troubles using an CryptoStream for my file encryption..

Code:

public static void EncryptFile(string inputFile, string outputFile)
    {
        int num;
        string s = "PUPlr";
        byte[] bytes = new UnicodeEncoding().GetBytes(s);
        string path = outputFile;
        FileStream stream = new FileStream(path, FileMode.Create);
        RijndaelManaged managed = new RijndaelManaged();
        CryptoStream crpytStream = new CryptoStream(stream, managed.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
        FileStream stream2 = new FileStream(inputFile, FileMode.Open);
        while ((num = stream2.ReadByte()) != -1)
        {
            crpytStream.WriteByte((byte)num);
        }
        stream2.Close();
        crpytStream.Close();
        stream.Close();
    }

Trying "managed.BlockSize = 16;" or "= 128;" doesn't seem to work, so how could I fix my error?

Best Solution

Block cyphers like Rijndael require keys and IVs of length equal to the block size, typically 256 bits.

In addition, the IV must be unique for each message, or your data will not be secure.