Question

I have a 3DES encrypted string from a service on java as -

30BA1A87B3B08F8A6F69BF0E2EC7539B

when i am applying 3DES encryption in PHP to check the result, i am getting a very different string which is as -

ªã;Îù1ù@yq—ÿÃÓ"Õó[ûñüM“ƒº5fá$!Ø5JºÝ7

i am using an open source PHP lib for encryption, which is Crypt_TripleDES from http://sourceforge.net/projects/phpseclib/.

Can someone help me, to understand what is wrong and where?

Please ask if I am missing anything.

Thanks

PHP Code -

require_once 'Crypt/TripleDES.php';
$tdes = new Crypt_TripleDES();
$tdes->setKey($key);
$enc_text = $tdes->encrypt($text);
echo 'Encrypted text - '.($enc_text).'<br />';
Was it helpful?

Solution

It is most like just how you are displaying the information.

In your first line, it appears you are outputting the string as hex. That is, each byte of the data is converted into two hexadecimal characters.

In your second line, it looks like you may just be trying to dump the raw binary to the output. That is, each byte is interpreted as an ASCII character, which makes sense why it looks like hell.

Can we get more information about your Java output? How did you get it exactly?


After looking at the library, it seems that yes, it is returning the raw binary string. To convert this to hex, you simply need to call the built-in bin2hex() function:

require_once 'Crypt/TripleDES.php';
$tdes = new Crypt_TripleDES();
$tdes->setKey($key);
$enc_text = $tdes->encrypt($text);
echo 'Encrypted text - ' . bin2hex($enc_text) . '<br />';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top