1

我有这个 Java 代码,我需要在 PHP 代码中对其进行转换,以获得相同的输出。你现在怎么办?

final byte[] key = {(byte)0x49, (byte)0x45, (byte)0x4D, (byte)0x4B, (byte)0x41, (byte)0x45, (byte)0x52, (byte)0x42, (byte)0x21, (byte)0x4E, (byte)0x41, (byte)0x43, (byte)0x55, (byte)0x4F, (byte)0x59, (byte)0x46};
final byte[] encRndB = {(byte)0x33, (byte)0xD4, (byte)0x8E, (byte)0xAF, (byte)0x75, (byte)0x70, (byte)0x40, (byte)0x5E};

final SecretKeySpec keySpec = new SecretKeySpec(key, "DESede");
final byte[] zeroInitVector = { 0, 0, 0, 0, 0, 0, 0, 0 };

final byte[] rndB      = tripleDes(encRndB, Cipher.DECRYPT_MODE, keySpec, zeroInitVector);

函数tripleDes是这样的:

public static byte[] tripleDes(final byte[] original, final int mode, final SecretKeySpec keySpec, final byte[] ivSpec) throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    final IvParameterSpec iv = new IvParameterSpec(ivSpec);
    cipher.init(mode, keySpec, iv);
    return cipher.doFinal(original);
}

rndB 应该是这样的:

{(byte)0x7B, (byte)0x8B, (byte)0xD0, (byte)0x72, (byte)0x0D, (byte)0xE8, (byte)0x5A, (byte)0x2B}

我试过这个:

<?php
function encryptText($plainText, $key) {
    $iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
    $padded = $plainText;

    $encText = mcrypt_decrypt("tripledes", $key, $padded, "cbc", $iv);

    return $encText;
}

$key = "\x49\x45\x4D\x4B\x41\x45\x52\x42\x21\x4E\x41\x43\x55\x4F\x59\x46";
$plainText = "\x33\xD4\x8E\xAF\x75\x70\x40\x5E";
print_r(unpack("H*", encryptText($plainText, $key)));
?>

但它并没有给我相同的输出......

4

0 回答 0