конвертировать php mcrypt_encrypt MCRYPT_3DES в Java

У меня есть код на PHP

$res = strtoupper(bin2hex(mcrypt_encrypt(MCRYPT_3DES, $this->hex2str($key), $this->hex2str($data), MCRYPT_MODE_ECB)));

public function hex2str($data) {
$len = strlen($data);
$res = pack("H" . $len, $data);
return $res;
}

Я пытаюсь создать в Java-версии.
Java-код:

private String doEncrypt3DES(String key, String data) throws Exception{
SecretKey secretKey;
byte[] keyValue;
Cipher c;

keyValue = Hex.decodeHex(key.toCharArray());
DESedeKeySpec keySpec = new DESedeKeySpec(keyValue);
secretKey = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);

// Create the cipher
c = Cipher.getInstance("DESede/ECB/NoPadding");

c.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] text = data.getBytes("utf-8"); // Base64.decodeBase64(data);
byte[] textEncrypt = c.doFinal(text);
String hex = bytesToHex(textEncrypt);
return hex;
}

Но они получили другой результат. Можете ли вы помочь мне исправить код Java?
данные : CED0CF172E8AC451B39FC746C5339F29

ключ : 436C6561724B657944657632536E724D436C6561724B6579

0

Решение

Используйте шестнадцатеричное декодирование, а не utf-8 декодирование данных.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector