字符串到字节数组,然后到 Java 中的 MD5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7930656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String to byte array and then to MD5 in Java
提问by Gray
in the last 5 hours im trying to do something that should be very simple and did it in like 10 minutes in C#, but no luck with Java. I got a 32 UpperCase and Numeric String (A-Z0-9), I need to convert this String to Dec, and then md5 it. My problem is that I dont have unsgined bytes so I cant md5 my array :\
在过去的 5 个小时里,我试图做一些应该非常简单的事情,并且在 C# 中用了大约 10 分钟就完成了,但在 Java 上却没有运气。我有一个 32 大写和数字字符串(A-Z0-9),我需要将此字符串转换为 Dec,然后 md5 。我的问题是我没有 unsgined 字节,所以我不能 md5 我的数组:\
Here is what I need to do in python:
这是我需要在python中做的事情:
salt = words[1].decode("hex")
passwordHash = generatePasswordHash(salt, pw)
generatePasswordHash(salt, password):
m = md5.new()
m.update(salt)
m.update(password)
return m.digest()
and here it is in C# :
这是在 C# 中:
public static string GeneratePasswordHash(byte[] a_bSalt, string strData) {
MD5 md5Hasher = MD5.Create();
byte[] a_bCombined = new byte[a_bSalt.Length + strData.Length];
a_bSalt.CopyTo(a_bCombined, 0);
Encoding.Default.GetBytes(strData).CopyTo(a_bCombined, a_bSalt.Length);
byte[] a_bHash = md5Hasher.ComputeHash(a_bCombined);
StringBuilder sbStringifyHash = new StringBuilder();
for (int i = 0; i < a_bHash.Length; i++) {
sbStringifyHash.Append(a_bHash[i].ToString("X2"));
}
return sbStringifyHash.ToString();
}
protected byte[] HashToByteArray(string strHexString) {
byte[] a_bReturn = new byte[strHexString.Length / 2];
for (int i = 0; i < a_bReturn.Length; i++) {
a_bReturn[i] = Convert.ToByte(strHexString.Substring(i * 2, 2), 16);
}
return a_bReturn;
}
I will be very happy to get a help with this :)
我会很高兴得到这方面的帮助:)
回答by JB Nizet
To parse a hex string into a byte : (byte) Integer.parseInt(s, 16)
.
要解析十六进制字符串成一个字节:(byte) Integer.parseInt(s, 16)
。
To transform your password string into a byte array, using the default encoding (which I suggest not to do : always specify a specific encoding) : password.getBytes()
(or password.getBytes(encoding)
for a specific encoding).
要将您的密码字符串转换为字节数组,请使用默认编码(我建议不要这样做:始终指定特定编码):(password.getBytes()
或password.getBytes(encoding)
用于特定编码)。
To hash a byte array : MessageDigest.getInstance("MD5").digest(byte[])
.
哈希的字节数组:MessageDigest.getInstance("MD5").digest(byte[])
。
To transform a byte to a hex String : See In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?
要将字节转换为十六进制字符串:请参阅在 Java 中,如何将字节数组转换为十六进制数字字符串,同时保留前导零?
回答by Gray
I believe that something like the following will work:
我相信像下面这样的东西会起作用:
// convert your hex string to bytes
BigInteger bigInt = new BigInteger(salt, 16);
byte[] bytes = bigInt.toByteArray();
// get the MD5 digest library
MessageDigest md5Digest = null;
try {
md5Digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// error handling here...
}
// by default big integer outputs a 0 sign byte if the first bit is set
if (bigInt.testBit(0)) {
md5Digest.update(bytes, 1, bytes.length - 1);
} else {
md5Digest.update(bytes);
}
// get the digest bytes
byte[] digestBytes = md5Digest.digest();
Here's more ideas for converting a hex string to a byte[]
array:
以下是将十六进制字符串转换为byte[]
数组的更多想法: