Encrypting with Perl CBC and decrypting with PHP mcrypt

Posted by Ed on Stack Overflow See other posts from Stack Overflow or by Ed
Published on 2010-04-09T14:24:54Z Indexed on 2010/04/09 15:13 UTC
Read the original article Hit count: 582

Filed under:
|
|
|

I have an encrypted string that was encrypted with Perl Crypt::CBC (Rijndael,cbc). The original plaintext was encrypted with encrypt_hex() method of Crypt::CBC.

$encrypted_string = '52616e646f6d49567b2c89810ceddbe8d182c23ba5f6562a418e318b803a370ea25a6a8cbfe82bc6362f790821dce8441a790a7d25d3d9ea29f86e6685d0796d';

I have the 32 character key that was used.

mcrypt is successfully compiled into PHP, but I'm having a very hard time trying to decrypt the string in PHP. I keep getting gibberish back.

If I unpack('H*', $encrypted_string), I see 'RandomIV' followed by what looks like binary.

I can't seem to correctly extract the IV and separate the actual encrypted message. I know I'm not providing my information, but I'm not sure where else to start.

$cipher = 'rijndael-256';
$cipher_mode = 'cbc';

$td = mcrypt_module_open($cipher, '', $cipher_mode, '');

$key = '32 characters'; // Does this need to converted to something else before being passed?
$iv = ??  // Not sure how to extract this from $encrypted_string.
$token = ?? // Should be a sub-string of $encrypted_string, correct?

mcrypt_generic_init($td, $key, $iv);
$clear = rtrim(mdecrypt_generic($td, $token), '');
mcrypt_generic_deinit($td); 
mcrypt_module_close($td);

echo $clear;

Any help, pointers in the right direction, would be greatly appreciated. Let me know if I need to provide more information.

© Stack Overflow or respective owner

Related posts about php

Related posts about mcrypt