我在爪哇运行了TILLE DES加密
null
四(我已经跑了)
cipher.getIV()
方法,实际上它的iv是空的),同样的字符串用php运行三重des加密。
无效的
但是我得到了不同的结果。为什么会这样?
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加密程序Iythi:我该怎么办?道具在哪里?
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);
}