0

I'm working on a project that requires me to cipher some data using Rijndael, CBC and PKCS7...

I have some examples that show me the initial data and the result I should get after applying Rijndael... I tried using .Net RijndaelManaged class but got nothing similar to the expected value and I believe it's because of some issue while converting my Byte array to String...

On the other hand, using BouncyCastle's Rijndael implementation I managed to get something very similar to the expected value, and the difference is that I can't set the PKCS7 padding mode... and my biggest issue is that I cannot find documentation anywhere!

Here's a very simplified version of my Encrypt function, notice I included my key and IV definitions here just for testing.

Public Shared Function Encrypt(data As Byte()) As Byte()
    Dim theCipher As New Org.BouncyCastle.Crypto.Engines.RijndaelEngine()
    Dim ciphr As New PaddedBufferedBlockCipher(New CbcBlockCipher(theCipher), new Pkcs7Padding())

    Dim key As New KeyParameter(System.Text.Encoding.UTF8.GetBytes("---exampleKey---"))
    Dim iv As Byte() = System.Text.Encoding.UTF8.GetBytes("---example-IV---")

    Dim IVKey As New ParametersWithIV(key, iv)
    ciphr.Init(True, IVKey)

    Dim size As Integer = ciphr.GetOutputSize(data.Length)
    Dim result(size) As Byte

    Dim oLen As Integer = ciphr.ProcessBytes(data, 0, data.Length, result, 0)
    oLen += ciphr.DoFinal(result, oLen)

    If oLen < size Then
        Dim tmp(oLen) As Byte
        Array.Copy(result, 0, tmp, 0, oLen)
        result = tmp
    End If

    Return result
End Function
  • I editted the function, I found the way to set the PKCS7Padding to the PaddedBufferedBlockCipher, but the result doesn't change! – Pablo Rodríguez Jun 03 '14 at 22:32
  • Please show us the outputs and the outputs you are expecting. If you think a byte[] / string conversion is the issue, please show that as well. Note that "something similar" is not a very good description, and with encryption, "something similar" may be way, way off :) – Maarten Bodewes Jun 03 '14 at 22:43
  • @owlstead Thanks for your comment, this is the actual data: input: `5400863131313334383030313030303634363835303030323031303130303236313230393131303130333233303130303030303030300A0A0A0A0A0A0A0A0A0A` where `0A0A0A0A0A0A0A0A0A0A` is the padding. key: `1x3oxy8pl0ri3jmh` iv: `X543a1K9mb6j4lP1` the expected result is `953EA291E9B2BFB1394C1571AB7D1923A456C516313143A8ADA21C81CAC14483F6E59EC916CBE5D6CEAD307BFB4D131DF03813E887BBF380F12877FBA647B157` and it is correct since I could replicate the operation in [http://aes.online-domain-tools.com] – Pablo Rodríguez Jun 03 '14 at 22:54
  • OK, I don't see anything particularly wrong in your code. If you do have a full match, do you still have a question? I cannot fix the documentation of bouncy as an answer, that would take 1 million answers or so, it's horrible :) – Maarten Bodewes Jun 03 '14 at 22:57
  • Well I still believe there's something missing or wrong in my code, this is the output I get: `953EA291E9B2BFB1394C1571AB7D1923A456C516313143A8ADA21C81CAC14483F6E59EC916CBE5D6CEAD307BFB4D131DF03813E887BBF380F12877FBA647B1570767DEACC0F3BFF64A1DD113EF54CBB600` – Pablo Rodríguez Jun 03 '14 at 23:00
  • The result on that web page is not padded. So you are looking at a full block of padding here that is encrypted, and - for some reason - a single byte still set to zero, it's initial value probably. Oh, yeah, that's the f*cked up way VB creates arrays , you need to do -1 there. – Maarten Bodewes Jun 03 '14 at 23:08
  • Didn't see your comment on this issue, but yes VB was not helping! – Pablo Rodríguez Jun 04 '14 at 00:07

1 Answers1

0

Basically your plain text is exactly N times the blocksize. This means that a full block of padding is added before encryption, resulting in an additional block of ciphertext. Furthermore, because the way VB creates arrays, you need to use Dim varName(size - 1) as Byte.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • I didn't think about that... and yes, VB is not very reasonable for anything... but it was not my choice! – Pablo Rodríguez Jun 03 '14 at 23:44
  • You were totaly right about the array definition, it helped! Now I guess I have to solve the extra padding block issue... Is it ok to assume that the PKCS7Padding will add that 10byte padding if I remove it from my initial data? – Pablo Rodríguez Jun 03 '14 at 23:53
  • And it was correct, correcting the array definition and excluding the padding from the data input did the trick! Thanks! – Pablo Rodríguez Jun 04 '14 at 00:05
  • Eh, sorry, I cannot parse that second comment. There is a 16 byte padding block added to the plaintext before CBC encryption. If you remove 10 bytes from the plaintext, then 10 bytes padding will be added, yes, and the there will be one block less. Padding is always at least one and max. blocksize (16) bytes. – Maarten Bodewes Jun 04 '14 at 05:45
  • Does this solve your issue, Pablo? Or do you need code? My VB is extremely rusty (or brand shining new, depending on how you look at it). – Maarten Bodewes Jun 04 '14 at 16:33
  • Yes! It's solved now! Thank you very much! Part of the problem was me not aware that the padding is always added and the the other issue was VB's "singular" way of defining arrays! – Pablo Rodríguez Jun 05 '14 at 06:14