简体   繁体   English

PHP中的3DES与Java不同

[英]3DES in PHP is not same as Java

I have a 3DES encrypted string from a service on java as - 我从java上的服务获得了3DES加密的字符串-

30BA1A87B3B08F8A6F69BF0E2EC7539B

when i am applying 3DES encryption in PHP to check the result, i am getting a very different string which is as - 当我在PHP中应用3DES加密来检查结果时,我得到了一个非常不同的字符串,即-

ªã;Îù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/ . 我正在使用开放源代码的PHP库进行加密,即http://sourceforge.net/projects/phpseclib/中的 Crypt_TripleDES。

Can someone help me, to understand what is wrong and where? 有人可以帮助我,了解问题所在和错误之处吗?

Please ask if I am missing anything. 请问我是否有任何遗漏。

Thanks 谢谢

PHP Code - PHP代码-

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

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. 也就是说,每个字节都被解释为ASCII字符,这使它看起来像地狱是合理的。

Can we get more information about your Java output? 我们可以获取有关您的Java输出的更多信息吗? 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: 要将其转换为十六进制,只需要调用内置的bin2hex()函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM