1

I have code in C# which produces MD5 encoded byte[] from String and then this byte[] is converted to String. The C# code is

byte[] valueBytes = (new UnicodeEncoding()).GetBytes(value);
byte[] newHash = (new MD5CryptoServiceProvider()).ComputeHash(valueBytes);

I need to get the same result in Java. I'm trying to do this

Charset utf16 = Charset.forName("UTF-16");
return new String(DigestUtils.md5(value.getBytes(utf16)), utf16);

The code is using Apache Commons Codec library for MD5 calculations. I'm using UTF16 charset because I've read in other SO questions that C#'s UnicodeEncoding uses it by default.

So the code snippets look like they do the same thing, but when I'm passing the string byndyusoft2014, C# gives me hV7u6mQYRgBXXF9jOWWYJg== and Java gives me ﹡둛뭶魙ꇥ늺ꢑ. I've tried UTF16LE and UTF16BE as charsets with no luck.

Has anyone idea about what I'm doing wrong?

2
  • 2
    C# gives me hV7u6mQYRgBXXF9jOWWYJg== No your code returns byte array. You convert it somewhere to base64 string. Do the same in hava too Commented Aug 28, 2017 at 15:10
  • 2
    What is byndyusoft2014? You shouldn't create a string out of the MD5 hash value bytes directly. You can use the md5Hex() method or the Base64 class to get a string representation for the bytes. And you should use the same type of string representation for the hash bytes in both languages. Commented Aug 28, 2017 at 15:23

2 Answers 2

1

I think because of the java decode string to byte[] with utf-8,but the C# is not.So the java and C# encode the different byte array,and then get the different result.You can decode the string to byte[] at c# with utf-8,and see the result.Like following code:

UTF8Encoding utf8 = new UTF8Encoding();
byte[] bytes=utf8.GetBytes("byndyusoft2014");
byte[] en=(new MD5CryptoServiceProvider()).ComputeHash(bytes);
Console.WriteLine(Convert.ToBase64String(en));

and the java code:

    byte[] en = DigestUtils.md5Digest("byndyusoft2014".getBytes());
    byte[] base64 = Base64Utils.encode(en);
    System.out.println(new String(base64));

Of course,in your description,the result of C# like be encoded with base64,so the java should encode the byte array with base64. The result of them is same as swPvmbGDI1GbPKQwL9knjQ==

The DigestUtils and Base64Utils is some implementation of MD5 and BAS64 in spring library

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

1 Comment

Thank you for the hint with Base64. It solved the problem.
0

As it turned out, the main difference was not presented in my original code snippet - it was convertation from MD5 encoded byte[] to String. You need to use Base64 to get final result. This is the working code snippet in Java

Charset utf16 = Charset.forName("UTF-16LE");
return new String(Base64.encodeBase64(DigestUtils.md5(value.getBytes(utf16))));

With this code I get the same result as with C#. Thank you all for good hints!

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.