0

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 />';
Pawan
  • 517
  • 1
  • 9
  • 25

1 Answers1

1

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 />';
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • the java is converting string to hex... but i have no clue how to do that – Pawan Jul 26 '12 at 04:01
  • Can you post a little code of how you're arriving at this string in PHP? What function are you calling exactly – Jonathon Reinhart Jul 26 '12 at 04:03
  • I'm not finding the origins of `Crypt_TripleDES` anywhere by googling. Is this a third party library? If so, do you have a link to their webpage or documentation? – Jonathon Reinhart Jul 26 '12 at 04:21
  • http://sourceforge.net/projects/phpseclib/ i am using lib from there... can you suggest me a way to convert binary to hex in php ... thanks – Pawan Jul 26 '12 at 07:37
  • @Pawan see my edit. I have not tested this, but I think it should work. Note that you could have saved yourself some time by just Googling for [`php convert to hex string`](http://www.google.com/search?q=php%20convert%20to%20hex%20string). `bin2hex` was the first result :-) – Jonathon Reinhart Jul 27 '12 at 00:08
  • Thanks @Jonathon, and sorry for all trouble... bin2hex was not producing the expected results, so i have installed java interpreter on the server and used the same java code which was doing encryption at other end. I have used that Java code in PHP to get the same result... now i don't need to worry if other party change their enc algo. i will simply get the latest code and use in my PHP script... problem solved :) – Pawan Jul 27 '12 at 04:06