java 生成的盐与生成的盐不同。网络sha-1
Salt generated by java is not same as generated by. Net sha-1
我正在将系统从 .Net 迁移到存储在数据库中的 Java 用户密码,问题是 java 与 .Net 中计算的哈希值不同 return。 Net 和问题是由于 java 中的字节被签名而在 c# 中未签名,所以知道如何使用 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;
}
incase 任何人需要类似的东西,两者结果相同但问题出在结果字符串编码中,两者应使用相同的编码 "i.e ISO-8859 or UTF8"。
我正在将系统从 .Net 迁移到存储在数据库中的 Java 用户密码,问题是 java 与 .Net 中计算的哈希值不同 return。 Net 和问题是由于 java 中的字节被签名而在 c# 中未签名,所以知道如何使用 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;
}
incase 任何人需要类似的东西,两者结果相同但问题出在结果字符串编码中,两者应使用相同的编码 "i.e ISO-8859 or UTF8"。