Java相当于php pack('H *',str)

时间:2013-09-22 13:16:34

标签: java php string

修改

我将$checksum = md5($someString+$bkey);更改为$checksum = md5($someString.$bkey);

我需要在Java中执行以下操作:

$hexString = '90aa';#sample value
$bkey = pack('H*',$hexString);
$someString='qwe';#sample value
$checksum  = md5($someString.$bkey);
echo $checksum;

我无法在Java中将hexString转换为bkey以获得与php脚本相同的结果。除bkey以外,一切正常。

如果我删除bkey,则

PHP:

$someString='qwe';#sample value
$checksum  = md5($someString);
echo $checksum;

结果:76d80224611fc919a5d54f0ff9fba446

爪哇:

String someString = "qwe";
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        String checksum = new BigInteger(1, messageDigest.digest(someString
                .getBytes())).toString(16);
        System.out.println(checksum);

结果:76d80224611fc919a5d54f0ff9fba446

如您所见,它可以正常工作

使用bkey

PHP:

$hexString = '90aa';#sample value
$bkey = pack('H*',$hexString);
$someString='qwe';#sample value
$checksum  = md5($someString.$bkey);
echo $checksum;

结果:18f5f1a9bf898131945dd9e315759fe4

爪哇:

public static void main(String[] args) throws NoSuchAlgorithmException {
        String hexString = "90aa";
        String bkey = hexToString(hexString);
        String someString = "qwe";
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        String input = someString + bkey;
        String checksum = new BigInteger(1, messageDigest.digest(input
                .getBytes())).toString(16);
        System.out.println(checksum);
    }
    public static String hexToString(String hex) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < hex.length(); i += 2) {
            String str = hex.substring(i, i + 2);
            output.append((char) Integer.parseInt(str, 16));
        }
        return output.toString();
    }

结果:44bb634dee436833dd65caa5043ffeb9

正如您所看到的结果不同。

如何将hex String转换为String以获得相同的结果?

1 个答案:

答案 0 :(得分:4)

问题实际上并不在您的Java代码中,而是在PHP代码中。

$checksum = md5($someString+$bkey);行没有按照您的想法执行,应该是:

$checksum  = md5($someString . $bkey);  # use concatenate, not sum

虽然,这提供了更好的PHP MD5,但没有帮助使Java代码与PHP匹配

修改

Java方面的问题在于字符编码。 Inoacked版本90aa的Java char值不是有效的unicode字符。因此,toByteArray(),方法可以做很多事情。如果您在字节级别处理所有Java代码(并忽略Java中任何字符中的任何高字节),那么您将得到与PHP相同的结果:

public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    String hexString = "90aa";
    byte[] bkey = hexToString(hexString);
    String someString = "qwe";
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    byte[] input = join(stringToBytes(someString), bkey);
    String checksum = new BigInteger(1, messageDigest.digest(input)).toString(16);
    System.out.println(checksum);
    System.out.println(Charset.defaultCharset().displayName());
}

private static byte[] join(byte[] a, byte[] b) {
    // join two byte arrays
    final byte[] ret = new byte[a.length + b.length];
    System.arraycopy(a, 0, ret, 0, a.length);
    System.arraycopy(b, 0, ret, a.length, b.length);
    return ret;
}

public static byte[] hexToString(String hex) {
    // hexToString that works at a byte level, not a character level
    byte[] output = new byte[(hex.length() + 1) / 2];
    for (int i = hex.length() - 1; i >= 0; i -= 2) {
        int from = i - 1;
        if (from < 0) {
            from = 0;
        }
        String str = hex.substring(from, i + 1);
        output[i/2] = (byte)Integer.parseInt(str, 16);
    }
    return output;
}
public static byte[] stringToBytes(final String input) {
    // unlike Stirng.toByteArray(), we ignore any high-byte values of the characters.
    byte[] ret = new byte[input.length()];
    for (int i = input.length() - 1; i >=0; i--) {
        ret[i] = (byte)input.charAt(i);
    }
    return ret;
}

上面的Java代码生成了MD5sum 18f5f1a9bf898131945dd9e315759fe4,这也是PHP提供的