2

The documentation says "If you use the following values, then you will get this result".

Block Size: 128 Cipher Mode: CBC Padding Mode: Zeros

Key: BTikvHBatPdAtgT3317QIQqGFY25WpIz

IV: a5Sg4U9m11Mw2tIZ

Value to be encyrpted: 2008-06-02 13:28:45,Statements,1234567,,06/01/2008,06/01/2008,0

And this how the encryption result should look like after all these values are used according the developer guides.

b97f3985245be1065604c5808cebaa2e15813bf938cfaa3c198fd464565c13ced7c91ac6 b68326ce5ed5e81cb7de81acb9fcd1b1636127efbac3203da5bdccea

However, I cannot get this result for some reason, this is the code I used to populate this result but it gives me same characters at first and later on the characters changes as you may see in the result at very bottom of this question.

This is the code and below is the new output:

public static void Main(string[] args)
{
  RijndaelManaged rij = new RijndaelManaged();
  rij.Mode = CipherMode.CBC;
  rij.Padding = PaddingMode.Zeros;
  rij.Key = ASCIIEncoding.UTF8.GetBytes("BTikvHBatPdAtgT3317QIQqGFY25WpIz");
  rij.IV = ASCIIEncoding.UTF8.GetBytes("a5Sg4U9m11Mw2tIZ");
  ICryptoTransform transform = rij.CreateEncryptor();

  byte[] data = Encoding.ASCII.GetBytes("2008-06-02 13:28:45,Statements,1234567,,06/01/2008,06/01/2008,0");
  byte[] result = transform.TransformFinalBlock(data, 0, data.Length);
  Console.WriteLine(ByteArrayToString(result));
}

public static string ByteArrayToString(byte[] ba)
{
  StringBuilder hex = new StringBuilder(ba.Length * 2);
  foreach (byte b in ba)
    hex.AppendFormat("{0:x2}", b);
  return hex.ToString();
}

My output :

b97f3985245be1065604c5808cebaa2e15813bf938cfaa3c198fd464565c13ced7c91ac6b68326ce5ed5e81cb7de81ac

Note: I needed to update the question as I advanced and found a better solution but the new problem has arose right now.

Tarik
  • 79,711
  • 83
  • 236
  • 349

2 Answers2

2

This key and IV you have look like base64 encoding...

rij.Key = Convert.FromBase64String("BTikvHBatPdAtgT3317QIQqGFY25WpIz");
rij.IV = Convert.FromBase64String("a5Sg4U9m11Mw2tIZ");
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • It throws this error "IV length is different than block size." – Tarik May 19 '12 at 00:40
  • Well, obviously they *are* of different length... which documentation are you referring to? Are you sure it's related to the .NET implementation of Rijndael? – Thomas Levesque May 19 '12 at 00:41
  • The documentation for the IV property says this: "The size of the IV property must be the same as the BlockSize property.". So obviously the example you have can't work. – Thomas Levesque May 19 '12 at 00:42
  • The documentation comes from a highly-regarded company which we are using to integrate our applications. So they need to be fine. – Tarik May 19 '12 at 00:43
  • Although it looks like Base64, the IV string has the correct size (16 bytes). The key is too long, but this shouldn't hurt. So I assume the example really expects those two values... – emboss May 19 '12 at 00:44
  • @Braveyard, I'm not saying this documentation is not fine, but perhaps it's related to another implementation. – Thomas Levesque May 19 '12 at 00:44
  • @ThomasLevesque: When I use `FromBase64String()` method, it produces completely different string but if I use `encoding.GetBytes()` method it produces correct one with missing bytes. – Tarik May 19 '12 at 00:52
2

You were talking about weird characters in your output - actually, the output you posted is hex encoding, so you should try to encode yours to hex, too, as explained here.

Edit:

I figured it out now. You need to additionally Close() either the stream or the writer - in CBC mode, this is needed to finalize the padding on the last block, if it is omitted then the last block will not be output. Next problem is that if you do this, then you will still end up with a slightly different output than the one expected in your example. Here's why: if you decrypt your example ciphertext, you will get:

2008-06-02 13:28:45,Statements,1234567,,06/01/2007,06/01/2007,0

It differs from your plaintext in that there's a 2007 at the end where you have 2008. If you fix this and properly close the stream then you will finally receive exactly the same output as in the example.

Community
  • 1
  • 1
emboss
  • 38,880
  • 7
  • 101
  • 108
  • The weird thing : "b97f3985245be1065604c5808cebaa2e15813bf938cfaa3c198fd464565c13ced7c91ac6b68326ce5ed5e81cb7de81ac" is what was produced. There are some bits missing. – Tarik May 19 '12 at 00:49
  • @Braveyard Hmm, that's exactly 32 characters, which corresponds to 16 bytes of output which is exactly one block. This means the final block is missing. You did flush the Writer, so that shouldn't be it... Thinking ...:) – emboss May 19 '12 at 00:54
  • What does `array.Length` say? – emboss May 19 '12 at 00:57
  • I tried to reproduce your code, with Ruby. The funny thing is that it's actually AES-256, and *not* 128. I assume that's because of the 32 bytes key instead of the required 16 bytes. You should get the same result as now if you set BlockSize to 256. But I can't see why there's any output missing, I get exactly the same result as you expected. Can't see what's wrong with your code right now. – emboss May 19 '12 at 01:27
  • If I change the BlockSize to 256, I receive this error "IV length is different than block size" – Tarik May 19 '12 at 01:28
  • Weird. It's definitely AES-256. But OK, that's a different story. Why won't that last block appear in your case?? – emboss May 19 '12 at 01:31
  • I wish I could know why. It just doesn't appear. – Tarik May 19 '12 at 01:32
  • Does it help if you call `Close()` on the writer or the stream before you convert the stream to an array? – emboss May 19 '12 at 01:40
  • I tried `Close()` and now the result changed but still not true : b97f3985245be1065604c5808cebaa2e15813bf938cfaa3c198fd464565c13ced7c91ac6b68326ce5ed5e81cb7de81ac36b1106392bd8303d2aedf2c26161a8a – Tarik May 19 '12 at 01:49