java des加密结果不一样_为什么在Java中执行DES加密的结果不同于在PHP中执行的结果?...

我在爪哇运行了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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值