0

I have a string encrypted with AES in Counter mode, it wasn't done with PHP and I can't make mcrypt to decode it :( there's a class that works as expected: http://www.movable-type.co.uk/scripts/aes.html (see bottom of the page), but it's slow so I want to decrypt with mcrypt.

According to classes decrypt method I did following:

 $key = $_POST['key'];

 $length = strlen($key);
 if($length > 32)
   $key = substr($key, 0, 32);

 $cyphered = base64_decode($_POST['cyphered']);
 /// make initialization vector with first 8 bytes treated as integers
 $f8b = array_map('ord', str_split(substr($cyphered, 0, 8)));
 array_unshift($f8b, 'I8');
 $iv = call_user_func_array('pack', $f8b);

 print mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, substr($cyphered, 8), 'ctr', $iv);

And result is garbage on output :( I don't understand what am I doing wrong. Could somebody please sched some light on this problem?

Daniel
  • 4,272
  • 8
  • 35
  • 48
  • You may have solved the problem by now, but I'm just curious what platform, and crypto library was the string originally encrypted with? – curtisdf May 17 '13 at 07:54

1 Answers1

0

phpseclib's pure PHP AES implementation is almost 4x as fast as movable type's AES implementation:

http://phpseclib.sourceforge.net/crypt/examples.html

And that's when mcrypt isn't available. When mcrypt is available it'll use that and be even faster.

That said, if you are insistent on using mcrypt... try MCRYPT_RIJNDAEL_128. MCRYPT_RIJNDAEL_256 has a block size of 256 bits, which works for Rijndael (Rijndael has a variable block size) but not AES.

  • 1. It didn't work out :( from what I see it uses IV = 16 bytes size, but class I've shown uses 8, 2. I can't drag in another dependency :( – Daniel Oct 09 '12 at 12:49
  • The IV has to be the equal to the block size. It's possible the class you've shown is null padding the IV. – neubert Oct 12 '12 at 19:59