1

I am doing md-5 hashing in both android and c# at the same time. But both the results should be the same for the same inputs. Is there any difference in the way its done in both the languages?

I get different outputs in both the cases. Here is the c# code for md-5 calculation:

//this method hashes the values sent to it using MD5
public static String hashwithmd5(String toHashMD5)
{
    byte[] keyArray;

    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(toHashMD5));
    hashmd5.Clear();
    return Convert.ToBase64String(keyArray, 0, keyArray.Length); 
}

and here is the code for md5 in android using bouncycastle

public byte[] Hashing(String toHash) throws Exception{
    byte[] hashBytes = toHash.getBytes("UTF-8");
    EditText et = (EditText) findViewById(R.id.entry);
    org.bouncycastle.crypto.digests.MD5Digest digest = new org.bouncycastle.crypto.digests.MD5Digest();
    digest.reset();
    digest.update(hashBytes, 0, hashBytes.length);
    int length = digest.getDigestSize();
    byte[] md5 = new byte[length];
    digest.doFinal(md5, 0);
    et.setText(md5.toString());
    return md5;
}

the result of md5 in c# is :XUFAKrxLKna5cZ2REBfFkg==

the result of md5 in android is :[B@4053cf40

3
  • can somebody tell me what is different in two of these computations, and how I should change it to get the same o/p? Commented Feb 15, 2011 at 0:10
  • and what if you compare the byte arrays? are they the same? Commented Feb 15, 2011 at 0:27
  • @cris can you get solution?" Commented Dec 18, 2013 at 9:23

4 Answers 4

8

The C# code converts the hash to Base64, the java code does not. If you convert both raw hashes to e.g. hex strings, they'll be the same.

Sign up to request clarification or add additional context in comments.

Comments

1

When you use this in Java:

byte[] md5 = new byte[length];
// ...
md5.toString()

you are not getting a representation of the byte values. You get the generic "string representation" of an object. Here, [B@4053cf40 basically means "array of bytes (that's for the '[B') which internally happens to be at address 4053cf40".

Use android.util.Base64 to convert your bytes to a Base64 encoded string.

1 Comment

Can you show me an example of how this library android.util.Base64 is used to convert a byte array into a base64 string?
0

@erik is correct. MD5 is no longer considered a "secure" hash; use SHA-256.

Comments

0

Erik is absolutely right. MD5 usage is near extinction, use any strong SHA

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.