I am converting strings to MD5 hashes using c#. I am then converting strings to MD5 hashes using Java. I have noticed that the results are not the same, even for the same input strings.
Here is my Java Code:
public String encrypt(String message)
{
String digest = null;
try
{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(message.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder(2*hash.length);
for(byte b : hash)
{
sb.append(String.format("%02x", b&0xff));
}
digest = sb.toString();
}
catch (Exception e)
{
//Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
return digest;
}
}
Here is my C# Code:
public static string Encrypt(string input)
{
Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
Byte[] hashedBytes = new MD5CryptoServiceProvider().ComputeHash(inputBytes);
return BitConverter.ToString(hashedBytes);
}
I have easier access to the Java code. Is there any way I could modify the Java code such that the resultant hashes are the same as the C# hashes?
Thank you in advance.