R
Ratfish
I've got a problem where a MD5 Hash function in .NET generates a
different hash than a similar routine in Java/Android. The Android
hash is calculated as "J0t��j#߸�bq-�",while the .NET hash is
calculated as "SjB0j+xqI9+4hxticS0Cjw==". Is this due to a character
set issue? Unicode versus non-unicode?
The code is below. Any suggestions would be greatly appreciated.
Aaron
=====================================
Here's the .NET code:
public static string EncodeText(byte[] key, string sText, Encoding
encoding)
{
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = encoding.GetBytes(sText);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
string sEncodedText = Convert.ToBase64String(encodedTextBytes);
return sEncodedText;
}
=====================================
Here's the Android code:
public class HMACMD5 {
private final String HMAC_MD5_NAME = "HmacMD5";
private SecretKeySpec sk;
private Mac mac;
public HMACMD5(byte[] key) throws GeneralSecurityException {
init(key);
}
public HMACMD5(String key) throws GeneralSecurityException {
init(EncodingUtils.getAsciiBytes(key));
}
private void init(byte[] key) throws GeneralSecurityException {
sk = new SecretKeySpec(key, HMAC_MD5_NAME);
mac = Mac.getInstance(HMAC_MD5_NAME);
mac.init(sk);
}
public byte[] ComputeHash(byte[] data) {
return mac.doFinal(data);
}
public byte[] ComputeHash(String data) {
return ComputeHash(EncodingUtils.getAsciiBytes(data));
}
}
public String encodeText(String sKey, String sSrc) throws Exception {
HMACMD5 hmacMD5 = new HMACMD5(sKey);
byte[] textBytes = EncodingUtils.getBytes(sSrc, "UTF-8");
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
return sEncodedText;
}
different hash than a similar routine in Java/Android. The Android
hash is calculated as "J0t��j#߸�bq-�",while the .NET hash is
calculated as "SjB0j+xqI9+4hxticS0Cjw==". Is this due to a character
set issue? Unicode versus non-unicode?
The code is below. Any suggestions would be greatly appreciated.
Aaron
=====================================
Here's the .NET code:
public static string EncodeText(byte[] key, string sText, Encoding
encoding)
{
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = encoding.GetBytes(sText);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
string sEncodedText = Convert.ToBase64String(encodedTextBytes);
return sEncodedText;
}
=====================================
Here's the Android code:
public class HMACMD5 {
private final String HMAC_MD5_NAME = "HmacMD5";
private SecretKeySpec sk;
private Mac mac;
public HMACMD5(byte[] key) throws GeneralSecurityException {
init(key);
}
public HMACMD5(String key) throws GeneralSecurityException {
init(EncodingUtils.getAsciiBytes(key));
}
private void init(byte[] key) throws GeneralSecurityException {
sk = new SecretKeySpec(key, HMAC_MD5_NAME);
mac = Mac.getInstance(HMAC_MD5_NAME);
mac.init(sk);
}
public byte[] ComputeHash(byte[] data) {
return mac.doFinal(data);
}
public byte[] ComputeHash(String data) {
return ComputeHash(EncodingUtils.getAsciiBytes(data));
}
}
public String encodeText(String sKey, String sSrc) throws Exception {
HMACMD5 hmacMD5 = new HMACMD5(sKey);
byte[] textBytes = EncodingUtils.getBytes(sSrc, "UTF-8");
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
return sEncodedText;
}