Java中字符串转字节数组再转MD5
在过去的 5 个小时里,我试图做一些应该非常简单的事情,并且在 C# 中花了大约 10 分钟就完成了,但在 Java 中却没有成功。 我有一个 32 位大写字母和数字字符串 (A-Z0-9),我需要将此字符串转换为 Dec,然后对其进行 md5。 我的问题是我没有未签名的字节,所以我无法 md5 我的数组:\
这是我需要在 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()
这里是 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;
}
我将很高兴获得这方面的帮助:)
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 :\
Here is what I need to do in 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# :
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将十六进制字符串解析为字节:
(byte) Integer.parseInt(s, 16)
。要将密码字符串转换为字节数组,请使用默认编码(我建议不要这样做:始终指定特定编码):
password.getBytes()
(或password.getBytes(编码)
用于特定编码)。对字节数组进行哈希处理:
MessageDigest.getInstance("MD5").digest(byte[])
。要将字节转换为十六进制字符串:请参阅在 Java 中,如何将字节数组转换为十六进制数字字符串,同时保留前导零?
To parse a hex string into a byte :
(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()
(orpassword.getBytes(encoding)
for a specific encoding).To hash a byte array :
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?
我相信类似下面的东西会起作用:
这里是将十六进制字符串转换为
byte[]
数组的更多想法:I believe that something like the following will work:
Here's more ideas for converting a hex string to a
byte[]
array:您可以通过应用位掩码在 java 中使用无符号数字。请在此处查看详细信息。
You can use unsigned numbers in java with applying bit masks. Take a look details here.