从字符串到十六进制 MD5 哈希并返回
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只是使用
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".
您尚未显示
toHexString
,但基本上您需要相反的等效项 - 查找名为fromHexString
的方法或类似的方法。基本上
String.getBytes()
执行正常的编码(在本例中为 UTF-8)。您希望将文本(任意二进制数据的文本表示形式)有效解码为byte[]
。Apache Commons Codec 有适当的方法 - API 并不理想,但它可以工作:
You haven't shown
toHexString
, but basically you need the reverse equivalent - look for a method calledfromHexString
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 abyte[]
.Apache Commons Codec has appropriate methods - the API isn't ideal, but it would work:
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.如果您不想使用库,可以使用我的十六进制解码器版本来完成此操作,
If you don't want use a library, here is how you can do it with my version of the hex decoder,