Essentially my problem is that I have a java application that uses SHA-1 encryption and I have a .Net 2.0 WebService that needs to encrypt the same way for user authentication (passwords are encrypted in the database)
Take care to notice the wonderful wack in the java code to add 256 to the byte value.
Essentially the problem is that when I create the ASCII byte array the values are the same:
Password = test;
Java:
[116, 101, 115, 116]
C#:
[116, 101, 115, 116]
The problem is that when java calls byte[] digest = algorithm.diges t(); I get a different value in C# when I call:
Any suggestions would be appreciated, I have been racking my brains for quite awhile.....
Java code:
Take care to notice the wonderful wack in the java code to add 256 to the byte value.
Essentially the problem is that when I create the ASCII byte array the values are the same:
Password = test;
Java:
[116, 101, 115, 116]
C#:
[116, 101, 115, 116]
The problem is that when java calls byte[] digest = algorithm.diges t(); I get a different value in C# when I call:
Code:
byte[] hash = sha1.ComputeHash(Encoding.ASCII.GetBytes(userPassword));
java: value hashing user password 'password' = 15baa161e4c9b91 3f13f1068212510 b16cf813311b17e e68fd8
c#: value hashing user password 'password'= 15baa161e4c9b91 3f13f1068212510 b16cf813311b17e e68fd815b1aa161 1e41c91b913f13f 10618212510b16c 1f813311b17e1e6 18f1d8
c#: value hashing user password 'password'= 15baa161e4c9b91 3f13f1068212510 b16cf813311b17e e68fd815b1aa161 1e41c91b913f13f 10618212510b16c 1f813311b17e1e6 18f1d8
Any suggestions would be appreciated, I have been racking my brains for quite awhile.....
Java code:
Code:
public static String encryptPassword(String password) {
String returnValue = null;
byte[] buf = password.getBytes();
MessageDigest algorithm=null;
try {
algorithm = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
sClassLog.logException(e);
}
algorithm.reset();
algorithm.update(buf);
byte[] digest = algorithm.digest();
returnValue = "";
for (int byteIdx = 0; byteIdx < digest.length; byteIdx++) {
returnValue += Integer.toHexString(digest[byteIdx] + 256);
}
return returnValue;
}
My C# Code:
private String encryptPassword(String userPassword)
{
SHA1 sha1 = SHA1CryptoServiceProvider.Create();
byte[] hash = sha1.ComputeHash(Encoding.ASCII.GetBytes(userPassword));
StringBuilder digest = new StringBuilder();
foreach (byte n in hash) digest.Append(Convert.ToInt32(n + 256).ToString("x2"));
return digest.ToString();
}
Comment