Iam正在将系统从.Net迁移到Java用户密码存储在数据库中,java不返回相同的问题Hashed值与.Net中计算的问题是由于java中的字节问题是在c#unsigned,所以任何想法如何用java解决这个问题?
public static string NetHash(int pSalt, string pPassword, string customerCode)
{
// Create Byte array of password string
ASCIIEncoding encoder = new ASCIIEncoding();
Byte[] secretBytes = encoder.GetBytes(pPassword + customerCode.ToLower());
// Create a new salt
Byte[] saltBytes = new Byte[4];
saltBytes[0] = (byte)(pSalt >> 24);
saltBytes[1] = (byte)(pSalt >> 16);
saltBytes[2] = (byte)(pSalt >> 8);
saltBytes[3] = (byte)(pSalt);
// append the two arrays
Byte[] toHash = new Byte[secretBytes.Length + saltBytes.Length];
Array.Copy(secretBytes, 0, toHash, 0, secretBytes.Length);
Array.Copy(saltBytes, 0, toHash, secretBytes.Length, saltBytes.Length);
SHA1 sha1 = SHA1.Create();
Byte[] computedHash = sha1.ComputeHash(toHash);
return encoder.GetString(computedHash);
}
同样改为Java,如下所示
public String javaHash(int pSalt, String pPassword, String customerCode) {
String result = "";
String s = pPassword + customerCode.toLowerCase();
byte[] secretBytes = s.getBytes();
byte[] saltBytes = new byte[4];
saltBytes[0] = (byte) (pSalt >> 24);
saltBytes[1] = (byte) (pSalt >> 16);
saltBytes[2] = (byte) (pSalt >> 8);
saltBytes[3] = (byte) (pSalt);
byte[] toHash = ArrayUtils.addAll(secretBytes, saltBytes);
try {
MessageDigest md = null;
md = MessageDigest.getInstance("SHA-1");
byte[] digest1 = md.digest(toHash);
result = new String(digest1);
} catch (NoSuchAlgorithmException e) {
LOGGER.error("Unable to check hash password for user [{}]", customerCode, e);
}
return result;
}
答案 0 :(得分:0)
任何人都需要类似的东西,两者都相同,但问题出在结果字符串编码中,两者都应使用相同的编码“即ISO-8859或UTF8”。