Method return the same hashcode in C # and java

0

I have the method in C #:

       private static string GetMD5(string strPlain)
    {
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] HashValue, MessageBytes = UE.GetBytes(strPlain);
        MD5 md5 = new MD5CryptoServiceProvider();
        string strHex = "";

        HashValue = md5.ComputeHash(MessageBytes);
        foreach (byte b in HashValue)
        {
            strHex += String.Format("{0:x2}", b);
        }
        return strHex;
    }

which generates an md5 of any string.

And I have a similar method in java

 public static String md5(final String s) {
    final String MD5 = "MD5";

    try {
        MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

But in java it is returning a different hashcode than in C #. It's the first time I use this and I'm not sure how it works. How can I solve this, preferably changing only the code in java? Using other external libraries is out of the question. The code in C # is being used in many places in the system and it would be impracticable to change it. The code in java is being created now and would have no problem moving it.

    
asked by anonymous 15.05.2018 / 20:56

0 answers