How do you conver this function?

Hi all,

I am having some difficulities with converting a function into Java. It is originally written in C# .Net. Below is C# code and result.

public string getMD5(string str)

{

Encoder encoder1 = Encoding.Unicode.GetEncoder();

byte[] buffer1 =newbyte[str.Length * 2];

char[] aa = str.ToCharArray();

encoder1.GetBytes(str.ToCharArray(), 0, str.Length, buffer1, 0,true);

byte[] buffer2 =new MD5CryptoServiceProvider().ComputeHash(buffer1);

StringBuilder builder1 =new StringBuilder();

for (int num1 = 0; num1 < buffer2.Length; num1++)

{

builder1.Append(buffer2[num1].ToString("X2"));

}

return builder1.ToString();

}

Input Parameter : 123123

Result is : 09E31D1AB4DE57A06DAC4834A7CB1432

But I cannot convert it into Java. Can someone pls help me to convert it into Java code using MD5 algorithm? I appreciate any help.

Thanks.

[1478 byte] By [GetAwaya] at [2007-10-3 7:53:12]
# 1
[url= http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html]java.security.MessageDigest[/url]
tschodta at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 2

Thanks for your reply. But of course I know how to use MD5 in java. I have written my own encrypt function using MD5. But I still cannot make it to produce this values. Here's my function I've written.

public java.lang.String getMD5(java.lang.String Password)

{

String strTemp = null;

byte[] bHash = new byte[Password.length() * 2];

try

{

MessageDigest md = MessageDigest.getInstance("MD5");

md.update(Password.getBytes());

bHash = md.digest();

StringBuffer hexString = new StringBuffer();

for(int intI = 0; intI < bHash.length; intI++)

{

strTemp = Integer.toHexString(bHash[intI]);

hexString.append(strTemp);

}

return hexString.toString();

}

catch(NoSuchAlgorithmException ns)

{

return "error";

}

}

Hopefully, someone can help me to translate the function with code.

GetAwaya at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 3

After your dismissive response to the post by 'tschodt' I'm not sure you deserve any help! It is not obvious from your original post that you knew about class MessageDigest.

The MD5 hash of 123123 is not 09E31D1AB4DE57A06DAC4834A7CB1432 but 4297f44b13955235245b2497399d7a93. The value was obtained using openssl command

echo -n 123123 | openssl md5 -hex

and confirmed using class MessageDigest.

I have checked the MD5 of 123123\n, 123123\r\n and 123123\r but none gives the value you have.

After further investigation, the MD5 you have is the MD5 of the UTF-16LE of "123123" i.e.

byte[] data = "123123".getBytes("UTF-16LE");

Message was edited by:

sabre150

sabre150a at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 4

Thanks sabre. Don't take it wrong about my previous post. I have read through that MD5 page again and again. But actually there was nothing about getByte() thingy there if I'm not wrong. Beside that I am with very tight schedule and I'm not java developer. I'm from Microsoft platform. So, I've a lot of problems with using this Creator 2 IDE and so on.

Anyway, I do appreciate both of ur help. I will try it and get back to you soon. Thank you again.

GetAwaya at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 5

Hi sarbe,

Thank you. The getByte("UTF-16LE") is correct solution. I have tested it and I got correct value. But however, from java function, I received encrypted string which is missing one "0" in the first position. Please take a look this:

(Java) "9E31D1AB4DE57A06DAC4834A7CB1432"

(.Net)"09E31D1AB4DE57A06DAC4834A7CB1432"

This is for 123123. But however, for other texts I have tested so far, they're correct. I do not understand why one zero is missing in the front for certain texts such as "123123".

Thank you again and much appreciate. I will try to study about these when I have finish this project.

Regards.

GetAwaya at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 6
Your Hex encyption routine is very poor. You can use class Hex from Jakarta Common Codec (Google will find it) or you can roll your own using a table of nibble characters - very easy.
sabre150a at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 7
> You can use class Hex from Jakarta Common Codec (Google will find> it) or you can roll your own using a table of nibble characters - very easy.Or with 1.5 use String.format("%32H", foo);(Warning: untested).
YAT_Archivista at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 8

> Or with 1.5 use String.format("%32H",

> foo);

(Warning: untested).

I don't think this works!

byte[] foo = {0,1,2,3,4,5,6,7};

System.out.println(String.format("%32H", foo));

produces a field of width32 but definitely not filled with "01020304050607".

sabre150a at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 9
I miscounted and thought the result fit in a long.
YAT_Archivista at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...
# 10
Thank you for all the help. Finally I have solved out the problem except that leading "0" missing in the front. Whatever encrypted value returned starts with "0", it just discards it. I am not sure why. Still trying to find out. Thanks.
GetAwaya at 2007-7-15 2:55:26 > top of Java-index,Other Topics,Algorithms...