Domanda

My exercise is to port an administration-backend from php to .net.

The backend communicates with an app written in java.

Some things compared with md5-hashes, in php and java the md5 hashes are equally.

I cannot change the md5 hash-code in the java app, because then will over 10k customer cards not work.

My problem is, the backend is ported and now the communication between the new backend (.net) and the java app.

My .net md5-hash code returns not the same hash as the java code.

java:

    public static String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");

            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);


            // Now we need to zero pad it if you actually want the full 32 chars.
            while (hashtext.length() < 32)
                hashtext = "0" + hashtext;
            return hashtext;
        }
        catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

and my .net code:

        public String hashMD5(String wert)
    {


        byte[] asciiBytes = ASCIIEncoding.UTF8.GetBytes(wert);
        byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
        string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();

        return hashedString;
    }

my java code returns for bb27aee4 :

46d5acfcd281bca9f1df7c9e38d50576

and my .net code returns:

b767fe33172ec6cbea569810ee6cfc05

I don't know what I have to do...

Please help and thanks in advance.

È stato utile?

Soluzione

Its not the problem of MD5 hash generator

MD5 hash for bb27aee4 : 46d5acfcd281bca9f1df7c9e38d50576 &
MD5 hash for BB27AEE4 : b767fe33172ec6cbea569810ee6cfc05

So basically in .NET you are generating MD5 hash for BB27AEE4 instead of bb27aee4

So check for the error in your code

Good Luck

Altri suggerimenti

The reason you're seeing different results from your hash is because the encoding used for your string differs. In your .NET code, you specify UTF8 explicitly, while there is no guarantee your Java code does the same; it may very well be using ASCII, which results in different hashes being generated.

If you explicitly specify the same encoding in both your .NET and Java code, you'll end up with the same MD5 hashes as well.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top