Rob Forrest
Rob Forrest

Reputation: 7450

PHP AES decryption

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?

Upvotes: 1

Views: 3548

Answers (1)

Javaguru
Javaguru

Reputation: 910

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

$realIV = base64_decode($iv);

Upvotes: 5

Related Questions