1

In .NET framework, I'm trying to encrypt some binary data (using RijndaelManaged), for decrypting I used the following sample code (C++/.NET):

RijndaelManaged^ rjdAlg= gcnew RijndaelManaged;
rjdAlg->IV= IV;
rjdAlg->Key=this->key;
ICryptoTransform^ decryptor= rjdAlg->CreateDecryptor();
MemoryStream^ msDecrypt = gcnew MemoryStream(encryptedData);
CryptoStream^ csDecrypt = gcnewCryptoStream(msDecrypt,decryptor,CryptoStreamMode::Read);
array<unsigned char>^ decryptedData= gcnew array<unsigned char>(encryptedData->Length);
csDecrypt->Read(decryptedData,0,decryptedData->Length);

Now, decryptedData contains useful data in addition to nulls (as the array initialization), how can I get the actual size of encrypted data ( if it was string I can just stop at the first null!).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hakim83
  • 59
  • 4

1 Answers1

0

You need to store the length separately, either as the first N bits/bytes of your encrypted data, or outside of the encrypted data.

Rijndael is a block cipher, which means it will always encrypt whole blocks, and you always get whole blocks back when decrypting.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • Thanks, but as I know the incomplete block will be padded (in my case with PKCS7 padding), this padding chars will be removed at decryption phase, I want to know if it is possible to know the size in the stream after decryption. – Hakim83 May 10 '11 at 07:50
  • No, that's what I tried to say in my answer. If you want the exact length, you need to store it in addition to the data you encrypt. There's no other way. – Lasse V. Karlsen May 10 '11 at 08:33