Sandeep
Sandeep

Reputation: 687

Different MD5 hashes for .net and java

`I want to encode my string using .net and pass it to a java program. I am using the same program for encryption and decryption on both the sides. But the java program and .net programs are returning different MD5 hash codes.

Here are the codes for both my programs :

VB.net

md5Provider = New MD5CryptoServiceProvider()
input = UTF8Encoding.UTF8.GetBytes(sSecurityKey)
oHash = oMd5CryptoServiceProvider.ComputeHash(input)

Java

MessageDigest m;
m = MessageDigest.getInstance("MD5");
byte[] UTF8bytes = key.getBytes("UTF8");
m.update(UTF8bytes,0,UTF8bytes.length);

Edit: The hash computed using vb.net is {170,226,106,33,183,52,99,154,17,225,135,139,170,141,116,108}

The m.digest() i get in java is {-44, 29, -116, -39, -113, 0, -78, 4, -23, -128, 9, -104, -20, -8, 66, 126}

The problem is that m.digest() from java and oHash from vb.net are different. So i cant go further with DES encryption.

Any help appreciated.

Upvotes: 0

Views: 1420

Answers (2)

Sandeep
Sandeep

Reputation: 687

At last I found the answer. I just converted the Md5 hash into BigInteger and then converted into an byte array. Now when i pass it as a key to the DES algorithm, i get my desired output.

Upvotes: 1

Alex Gitelman
Alex Gitelman

Reputation: 24732

You may want to compare input from both digest methods byte by byte. That is input from VB.net and UTF8bytes from Java. Most likely they will be different (could be some encoding specifics of VB.net, for example). Also, how do you compare results?

Upvotes: 3

Related Questions