0

I've compiled some AES implementation code from this site, it's supposed to perfrom a 128 bits key encryption. I tested the encryption/decryption programs which work OK together.

However if I encrypt anything with the mentioned code and then try to decrypt it by openssl tool built-in in linux, I just can't get decrypt it, it even logs me the bad magic number error. Same, if I encrypt anything with openssl and try to decrypt with the code won't work. I tried with both cbc ecb.

If they're both implementing AES, shouldn't it work same way?

diegoaguilar
  • 8,179
  • 14
  • 80
  • 129
  • There is far too little information to know for sure. There are different modes of AES, such as CBC or CFB, what kind of padding (null, PKCS7, etc), and the data going in. Are the Keys and Initialization vectors the same? Posting the relevant code, and showing how you are using OpenSSL, should help diagnose. – vcsjones Jul 08 '13 at 00:26
  • 1
    Ok, you're right, I'm gonna do some edits ... – diegoaguilar Jul 08 '13 at 00:27
  • So far, I give more info here: http://stackoverflow.com/questions/17517156/different-encrypted-outputs-in-aes-implementations?rq=1 – diegoaguilar Jul 08 '13 at 00:28
  • Is this a duplicate question of that one? – vcsjones Jul 08 '13 at 00:29
  • I just tried to try to ask *simpler* in here. Sorry if it might be duplicated. – diegoaguilar Jul 08 '13 at 00:31
  • 1
    AES is just the low-level algorithm. There is also the choice of key encoding, padding, block chaining modes, and so on. If those choices are different, then the implementations won't be directly compatible. – David Schwartz Jul 08 '13 at 00:34
  • @vcsjones, I a few basic edits. Thanks – diegoaguilar Jul 08 '13 at 00:41
  • 1
    it looks like the C code is using ECB and does no padding. so try encrypting a message (a multiple) of 16 bytes followed by 16 bytes of value 16 (pkcs#7 padding). or use a message (a multiple) of 16 bytes and `--nopad` in openssl (more likely to work). also, use -ecb-128 or whatever it's called. – andrew cooke Jul 08 '13 at 00:42
  • @andrewcooke You can always give it as an answer, you might win the correct one, hehe! I'm gonna try it out – diegoaguilar Jul 08 '13 at 00:47

1 Answers1

1

it looks like the C code is using ECB and does no padding. so try encrypting a message (a multiple) of 16 bytes followed by 16 bytes of value 16 (pkcs#7 padding). or use a message (a multiple) of 16 bytes and --nopad in openssl (more likely to work). also, use aes-128-ecb or whatever it's called.

a block cipher works on "chunks" of text - in this case, it's 16 characters long. so if you don't want to worry about padding you need to give an exact number of chunks.

also, ecb mode (doing each chunk in turn with no extra processing) isn't secure for many uses. see the wikipedia article (look at the penguin photos).

[edit:] [edit 2:]

> echo -n "abcdabcdabcdabcd" > msg
> wc msg
 0  1 16 msg
> openssl enc -aes-128-ecb -nopad -in msg -K 0 -S "" -iv ""
[noise]
> openssl enc -aes-128-ecb -nopad -in msg -K 0 -S "" -iv "" | wc
 0 1 16

try the above yourself and see if the other code decrypts it (edit 2 sets the key explicitly, and removes IV and salt - not sure what the latter two are for in this case).

[edit 3:]

as far as i can tell, the problem is related to the way that the password is converted to a key. openssl seems to be doing something extra that i can't get rid of unless i specify a key as hex (-K 0). and if i do that, the other program doesn't work (needs a password).

sorry, i'm out of ideas.

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
  • Sorry if it's stupid asking it, HOW can I do the multiple of 16 bytes message and the 16 bytes of value 16? – diegoaguilar Jul 08 '13 at 00:49
  • Tried with `openssl aes-128-ecb -nopad -in msg -out output` and got: bad decrypt 139963007211200:error:0607F08A:digital envelope routines:EVP_EncryptFinal_ex:data not multiple of block length:evp_enc.c:414: – diegoaguilar Jul 08 '13 at 00:56
  • msg must be exactly 16 characters. for example (without quotes and no return) "abcdabcdabcdabcd" – andrew cooke Jul 08 '13 at 01:00
  • No results so far. I tried with: `openssl enc -aes-128-ecb -nopad -nosalt -in msg -k hola -out a` and got a 16 bytes output. But the *other* code isn't decrypting it good – diegoaguilar Jul 08 '13 at 01:27