为什么Java和PHP执行的DES加密结果不一样?
Why did the result of DES Encryption executed in Java different from executed in PHP?
我 运行 Java 中的 Trible DES 加密,带有 null
IV(我有 运行 cipher.getIV()
方法,实际上它的 IV 为空)和相同的字符串 运行 PHP 中的三重 DES 加密和 null
IV,但我得到不同的结果。这是为什么?
Java代码:
private static final String model = "DESede/ECB/PKCS5Padding";
public static String desEncrypt(String message, String key) throws Exception {
byte[] keyBytes = null;
if(key.length() == 16){
keyBytes = newInstance8Key(ByteUtil.convertHexString(key));
} else if(key.length() == 32){
keyBytes = newInstance16Key(ByteUtil.convertHexString(key));
} else if(key.length() == 48){
keyBytes = newInstance24Key(ByteUtil.convertHexString(key));
}
SecretKey deskey = new SecretKeySpec(keyBytes, "DESede");
Cipher cipher = Cipher.getInstance(model);
cipher.init(1, deskey);
return ByteUtil.toHexString(cipher.doFinal(message.getBytes("UTF-8")));
}
PHP代码:
// composer require phpseclib/phpseclib
use phpseclib\Crypt\TripleDES;
function desEncrypt($str,$key){
$cipher = new TripleDES();
$cipher->setKey(hex2bin($key));
$cryptText = $cipher->encrypt($str);
return unpack("H*",$cryptText)[1];
}
我想修改我的PHP代码以适应Java加密过程,我应该怎么做?问题出在哪里?
Java 加密结果:
before: 622700300000
key: 0123456789ABCDEFFEDCBA98765432100123456789ABCDEF
after: c9aa8ebfcc12ce13e22a33b05d4c18cf
PHP 加密结果:
before: 622700300000
key: 0123456789ABCDEFFEDCBA98765432100123456789ABCDEF
after: a6e7a000d4ce79ac8b3db9f6acf73de3
固定PHP代码:
/**
* Triple DES (ECB) Encryption Function
* PKCS5Padding
*
* @param string $message String needed to be encode
* @param string $key Hex encoded key
* @return string Hex Encoded
*/
function desEncrypt($message,$key){
$cipher = new TripleDES(TripleDES::MODE_ECB);
$cipher->setKey(hex2bin($key));
$cryptText = $cipher->encrypt($message);
return bin2hex($cryptText);
}
您忘记在使用前对密钥进行十六进制解码。您还使用了 CBC 模式而不是 ECB 模式,但是由于您的 IV 全部为零,因此对于加密的第一个数据块,这相当于相同的事情。
我 运行 Java 中的 Trible DES 加密,带有 null
IV(我有 运行 cipher.getIV()
方法,实际上它的 IV 为空)和相同的字符串 运行 PHP 中的三重 DES 加密和 null
IV,但我得到不同的结果。这是为什么?
Java代码:
private static final String model = "DESede/ECB/PKCS5Padding";
public static String desEncrypt(String message, String key) throws Exception {
byte[] keyBytes = null;
if(key.length() == 16){
keyBytes = newInstance8Key(ByteUtil.convertHexString(key));
} else if(key.length() == 32){
keyBytes = newInstance16Key(ByteUtil.convertHexString(key));
} else if(key.length() == 48){
keyBytes = newInstance24Key(ByteUtil.convertHexString(key));
}
SecretKey deskey = new SecretKeySpec(keyBytes, "DESede");
Cipher cipher = Cipher.getInstance(model);
cipher.init(1, deskey);
return ByteUtil.toHexString(cipher.doFinal(message.getBytes("UTF-8")));
}
PHP代码:
// composer require phpseclib/phpseclib
use phpseclib\Crypt\TripleDES;
function desEncrypt($str,$key){
$cipher = new TripleDES();
$cipher->setKey(hex2bin($key));
$cryptText = $cipher->encrypt($str);
return unpack("H*",$cryptText)[1];
}
我想修改我的PHP代码以适应Java加密过程,我应该怎么做?问题出在哪里?
Java 加密结果:
before: 622700300000
key: 0123456789ABCDEFFEDCBA98765432100123456789ABCDEF
after: c9aa8ebfcc12ce13e22a33b05d4c18cf
PHP 加密结果:
before: 622700300000
key: 0123456789ABCDEFFEDCBA98765432100123456789ABCDEF
after: a6e7a000d4ce79ac8b3db9f6acf73de3
固定PHP代码:
/**
* Triple DES (ECB) Encryption Function
* PKCS5Padding
*
* @param string $message String needed to be encode
* @param string $key Hex encoded key
* @return string Hex Encoded
*/
function desEncrypt($message,$key){
$cipher = new TripleDES(TripleDES::MODE_ECB);
$cipher->setKey(hex2bin($key));
$cryptText = $cipher->encrypt($message);
return bin2hex($cryptText);
}
您忘记在使用前对密钥进行十六进制解码。您还使用了 CBC 模式而不是 ECB 模式,但是由于您的 IV 全部为零,因此对于加密的第一个数据块,这相当于相同的事情。