开发者

rijndael encryptor valid byte sizes

I have the following code:

var symmetricKey = Rijndael.Create().CreateEncryptor(key, iv);

key and iv are the same length, and they are byt开发者_JAVA百科e arrays of length 32. In runtime, this snippet fails, saying the length isn't supported. I also tried with 16. doesn't work either.

Am I using this wrong? what gives?


It should work correctly if you set the IV to 16 bytes, matching Rijndael's default block size.

(Either that or change the BlockSize itself to be compatible with the size of your IV.)


This code should work as an example:

        byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
        byte[] iv = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

        byte[] data = { 1, 2, 3, 4, 5 };   

        using (var symmetrickey = Rijndael.Create().CreateEncryptor(key, iv))
        {
            using (Stream f = File.Create("anencryptedfile.bin"))
            {
                using (Stream c = new CryptoStream(f, symmetrickey, CryptoStreamMode.Write))
                {
                    c.Write(data, 0, data.Length);
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜