views:

77

answers:

1
+1  Q: 

PHP AES decryption

Hi All,

I'm really struggling to decrypt a given cipher text in PHP and I'm hoping that one of you will be able to spot where I'm going wrong with this one.

So this is the string I'm trying to decode

Wq+J2IlE7Cug1gJNiwy1beIcFggGhn+gZHkTCQ4J/DR3OY45OMs4RXN850xbfAmy

I know its encrypted with AES/CBC/PKCS5Padding with an IV of

2ZqVSHjqn3kMump0rvd8AA==

and I'll email you the key upon request.

public static function aes128cbcDecrypt($key, $encrypted_text, $iv) 
{
  $td = mcrypt_module_open(MCRYPT_RIJNDAEL_192, '', MCRYPT_MODE_CBC, '');
  mcrypt_generic_init($td, $key, $iv);
  $decrypted = mdecrypt_generic($td, $encrypted_text);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
  return trim($decrypted);
}

When I've tried running this I get complete gibberish

ã5‡³/.3p›¤ý°$² —ïÅ»<9 ‘m ê|Ÿ.ÂYº|Šû^w¬¾‚=l“½·F›VársT~ê H�ÿfß]7ñH

Equally, when I encrypt the known plain text, I get a load of gibberish instead of the ascii string that its meant to be.

† —4†º¢V�Öæƒ{ Zsöœl ò°Þ 
PegöE&£á=Ǻ܀

Have any of you got any ideas as where I'm going wrong?

Many thanks

Rob

+2  A: 

Maybe a stupid question, but should the IV not be base64-decoded before it is used?

$realIV = base64_decode($iv);
Javaguru
You might well be right but that hasn't solved the problem.So inserting that line and using the decoded IV leaves be with an error"The IV parameter must be as long as the blocksize"So I've changed it to MCRYPT_RIJNDAEL_128 to solve that problem but still end up with gibberish.Ø;xšë4L-üôÙ´�C»îÐŽÃÒdϨ; Ï0Ü›Â9Œó¢¤Üž×Ò{Thanks for you help though.
Rob Forrest
So Base64 encode/decode was the answer.I wasn't decoding the key, IV, nor the cipher text. I guess that's what all the gibberish was about.Thanks for your help.
Rob Forrest