$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;
Run Code Online (Sandbox Code Playgroud)
我无法在 Java 中将 hexString 转换为 bkey 以获得与 php 脚本相同的结果。除了bkey一切正常。
如果我删除bkey然后:
PHP:
$someString='qwe';#sample value
$checksum = md5($someString);
echo $checksum;
Run Code Online (Sandbox Code Playgroud)
结果: 76d80224611fc919a5d54f0ff9fba446
爪哇:
String someString = "qwe";
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
String checksum = new BigInteger(1, messageDigest.digest(someString
.getBytes())).toString(16);
System.out.println(checksum);
Run Code Online (Sandbox Code Playgroud)
结果: 76d80224611fc919a5d54f0ff9fba446
如您所见,它有效
与bkey:
PHP:
$hexString = '90aa';#sample value
$bkey = pack('H*',$hexString);
$someString='qwe';#sample value
$checksum = md5($someString.$bkey);
echo $checksum;
Run Code Online (Sandbox Code Playgroud)
结果: 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();
}
Run Code Online (Sandbox Code Playgroud)
结果: 44bb634dee436833dd65caa5043ffeb9
如您所见,结果是不同的。
如何转换hex String为String获得相同的结果?
问题实际上不在于您的 Java 代码,而在于 PHP 代码。
该行$checksum = md5($someString+$bkey);没有做你认为它做的事情,它应该是:
$checksum = md5($someString . $bkey); # use concatenate, not sum
Run Code Online (Sandbox Code Playgroud)
虽然,这提供了更好的 PHP MD5,但无助于使 Java 代码与 PHP 匹配
编辑
Java 方面的问题在于字符编码。inoacked 版本的 Java 字符值90aa不是有效的 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;
}
Run Code Online (Sandbox Code Playgroud)
上面的 Java 代码生成 MD5sum 18f5f1a9bf898131945dd9e315759fe4,这也是 PHP 给出的