编辑
我$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
获得相同的结果?