从字符串到十六进制 MD5 哈希并返回

发布于 2024-09-05 10:55:13 字数 888 浏览 6 评论 0原文

我在java中有这个伪代码:

bytes[] hash = MD5.hash("example");

String hexString = toHexString(hash); //This returns something like a0394dbe93f

bytes[] hexBytes = hexString.getBytes("UTF-8");

现在,hexBytes[]hash[]是不同的。

我知道我做错了什么,因为 hash.length() 是 16,而 hexBytes.length() 是 32。也许它与 java 使用 Unicode 有关字符(这里只是一个疯狂的猜测)。

无论如何,问题是:如何从 hexString 中获取原始的 hash[] 数组。

如果你想查看的话,整个代码就在这里(大约 40 LOC)http://gist.github。 com/434466

该代码的输出是:

16
[-24, 32, -69, 74, -70, 90, -41, 76, 90, 111, -15, -84, -95, 102, 65, -10]
32
[101, 56, 50, 48, 98, 98, 52, 97, 98, 97, 53, 97, 100, 55, 52, 99, 53, 97, 54, 102, 102, 49, 97, 99, 97, 49, 54, 54, 52, 49, 102, 54]

非常感谢!

I have this pseudo-code in java:

bytes[] hash = MD5.hash("example");

String hexString = toHexString(hash); //This returns something like a0394dbe93f

bytes[] hexBytes = hexString.getBytes("UTF-8");

Now, hexBytes[] and hash[] are different.

I know I'm doing something wrong since hash.length() is 16 and hexBytes.length() is 32. Maybe it has something to do with java using Unicode for chars (just a wild guess here).

Anyways, the question would be: how to get the original hash[] array from the hexString.

The whole code is here if you want to look at it (it's ~ 40 LOC) http://gist.github.com/434466

The output of that code is:

16
[-24, 32, -69, 74, -70, 90, -41, 76, 90, 111, -15, -84, -95, 102, 65, -10]
32
[101, 56, 50, 48, 98, 98, 52, 97, 98, 97, 53, 97, 100, 55, 52, 99, 53, 97, 54, 102, 102, 49, 97, 99, 97, 49, 54, 54, 52, 49, 102, 54]

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落叶缤纷 2024-09-12 10:55:13

您只是使用 hexString.getBytes("UTF-8"); 获取十六进制字符串的字节,而不是将十六进制数字转换为其字节值。

也就是说,您需要编写 toHexString 函数的逆函数。
您的 toHexString 可能应该确保格式化低于 10 到 2 位的值,因此例如字节 9 最终为“09”而不是“9”。

You are just getting the bytes of the hex string with hexString.getBytes("UTF-8"); , not converting the hex digits to their byte values.

That is, you need to write the reverse of your toHexString function.
Your toHexString should probably make sure to format values below 10 to 2 digits, so e.g. the byte 9 ends up as "09" and not "9".

春花秋月 2024-09-12 10:55:13

您尚未显示 toHexString,但基本上您需要相反的等效项 - 查找名为 fromHexString 的方法或类似的方法。

基本上 String.getBytes() 执行正常的编码(在本例中为 UTF-8)。您希望将文本(任意二进制数据的文本表示形式)有效解码byte[]

Apache Commons Codec 有适当的方法 - API 并不理想,但它可以工作:

byte[] data = ...;
String hex = Hex.encodeHexString(data);
...

byte[] decoded = (byte[]) Hex.decode(hex);

You haven't shown toHexString, but basically you need the reverse equivalent - look for a method called fromHexString or something similar.

Basically String.getBytes() performs a normal encoding (in this case in UTF-8). You want to effectively decode the text - which is a textual representation of arbitrary binary data - into a byte[].

Apache Commons Codec has appropriate methods - the API isn't ideal, but it would work:

byte[] data = ...;
String hex = Hex.encodeHexString(data);
...

byte[] decoded = (byte[]) Hex.decode(hex);
燃情 2024-09-12 10:55:13

getBytes() 不解析十六进制字符,它处理字符编码。换句话说,它不会将 '0A' 转换为 0x0A,而是转换为 0x30 0x41,因为这就是字符 '0' 和 'A' 的编码方式。您需要在函数中使用 Integer.parseInt(String, radix) 代替,且 radix==16。

getBytes() doesn't parse hexadecimal characters, it processes character encodings. In other words, it doesn't turn '0A' into 0x0A, but into 0x30 0x41, because that's how the characters '0' and 'A' are encoded. You want Integer.parseInt(String, radix) instead in your function, with radix==16.

下壹個目標 2024-09-12 10:55:13

如果您不想使用库,可以使用我的十六进制解码器版本来完成此操作,

byte[] hexBytes = dehexify(hexString);

public static byte[] dehexify(String hexString) {
    if (hexString.length()%2 == 1)
        throw new IllegalArgumentException("Invalid length");       
    int len = hexString.length()/2;
    byte[] bytes = new byte[len];
    for (int i=0; i<len; i++) {
        int index = i*2;
        bytes[i] = (byte)Integer.parseInt(hexString.substring(index, index+2), 16);
    }
    return bytes;
}

If you don't want use a library, here is how you can do it with my version of the hex decoder,

byte[] hexBytes = dehexify(hexString);

public static byte[] dehexify(String hexString) {
    if (hexString.length()%2 == 1)
        throw new IllegalArgumentException("Invalid length");       
    int len = hexString.length()/2;
    byte[] bytes = new byte[len];
    for (int i=0; i<len; i++) {
        int index = i*2;
        bytes[i] = (byte)Integer.parseInt(hexString.substring(index, index+2), 16);
    }
    return bytes;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文