What does RijndaelManaged encryption do with invalid key sizes
Asked Answered
A

0

0

We are trying to integrate with a legacy c# application that uses RijndaelManaged for symmetric encryption. However it appears that they have used a 13 byte string as an encryption key!

The code is basically:

var initVectorBytes = Encoding.ASCII.GetBytes("16-char string");
var keyBytes = Encoding.ASCII.GetBytes("13-char string");
var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC };
var decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
var memoryStream = new System.IO.MemoryStream(encryptedbytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
....

In theory this shouldn't work - the docs clearly say "The key size must be 128, 192, or 256 bits" and when we try this (on a Xamarin/Mono compiler - don't have easy access to .net at the moment) it throws an exception.

But it apparently works on the legacy system, and they have unit tests that also call CreateDecryptor with a 13 byte key; so presumably a real .net system does somehow do something with this code. (I note that the docs for .net version 2.0 don't talk about key length restrictions - the code is compiled using .net 3.5 however)

Is it possible that it uses the Rijndael algorithm with a 104 byte key and block size? Or would it somehow pad the key or something?

Alfieri answered 15/1, 2015 at 13:38 Comment(3)
1) It does something undocumented, buggy and nonsensical. 2) If possible, decrypt the all data and then replace the encryption function in both the old and the new system with something standard and secure.Untutored
"Or would it somehow pad the key or something?" - the key is stretched if the key is smaller than 16 bytes, see private void GenerateKeyExpansion(byte[] rgbKey). When it's larger than 16 and not in (24, 32), it'll throw as far as I understand the decompiled .NET 2.0 assembly. Also how you compile it doesn't matter, the implementation is in the CLR. So it's true that you see different behavior on different runtimes.Imperator
"compiled" was indeed the wrong term :) Thanks for the pointer to GenerateKeyExpansion - I also found #15260899 which seems to be similar.Alfieri

© 2022 - 2024 — McMap. All rights reserved.