Re: PHP to decode AES

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Oct 18, 2012, at 4:39 PM, Adam Richardson <simpleshot@xxxxxxxxx> wrote:

> On Thu, Oct 18, 2012 at 12:06 PM, Rick Dwyer <rpdwyer@xxxxxxxxxxxxx> wrote:
>> Hello all.
>> 
>> Has anyone ever tried to decode a JAVA AES/CBC encrypted string with PHP before?
>> 
>> I found a tutorial online with the following code to use as starting point, but it fails to return anything readable:
>> 
>> $code ='Hello World';
>> $key = 'my key';
>> 
>> function decrypt($code, $key) {
>> $key = hex2bin($key);
>> $code = hex2bin($code);
>> $td = mcrypt_module_open("rijndael-128", "", "cbc", "");
>> mcrypt_generic_init($td, $key, "fedcba9876543210");
>> $decrypted = mdecrypt_generic($td, $code);
>> mcrypt_generic_deinit($td);
>> mcrypt_module_close($td);
>> return utf8_encode(trim($decrypted));
>> }
>> 
>> 
>> function hex2bin($hexdata) {
>> $bindata = "";
>> for ($i = 0; $i < strlen($hexdata); $i += 2) {
>> $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
>> }
>> return $bindata;
>> }
>> echo decrypt($code, $key);
>> 
>> The above returns output containing a series of unprintable characters.
>> 
>> I thought maybe it was due to $code not being in a hex format, but after converting to hex and resubmitting, I still unprintable characters.
>> 
>> Any info is appreciated.
> 
> Can you post the Java code you're using? There are things such as the
> padding specification that could cause some issues.
> 
> Adam
> 

Hi all.  We were able to get it to work.  But thank you for your replies.

But for anyone interested (including anyone who has emailed me privately implying I was up to something untoward), the specs from the client were as follows:

Unencrypted: "2012-10-18T10:57:43+0200 someurl.com"
Encrypted + base64: "ch7WvaSrCiHLstNeNUp5SkPfPGqZ8vrNPJT+9vU7jN/C"
Encrypt algorithm: AES/CBC, PKCS5Padding, IV of 16 NULL (0x0 hex) bytes
Key: "someKEY123-12346"
--------------------------------------------------------------------------------------------------------------------


$iv = mcrypt_create_iv(32);
$key = 'someKEY123-12346';
$text = '2012-10-18T10:57:43+0200 someurl.com';
$size = mcrypt_get_block_size('rijndael-128', 'cbc');
$text = pkcs5_pad($text, $size); 
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);

$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode('ch7WvaSrCiHLstNeNUp5SkPfPGqZ8vrNPJT+9vU7jN/C'), MCRYPT_MODE_CBC, $iv);


function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}


echo $decrypttext;
… 




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux