MD5 uses iso-8859-1 string hash in Java

I am implementing a digital payment service interface called Suomen verkkomaksut Information about payment will be sent to them in HTML form In order to ensure that no one is confused about the information during transmission, the MD5 hash is calculated with a special key not sent to them at both ends

My problem is that for some reason, they seem to decide that the incoming data is encoded in iso-8859-1 rather than UTF-8 The hash I send them is calculated using UTF - 8 string, so it is different from the hash value they calculate

I try the following code:

String prehash = "6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ|13466|123456||Testitilaus|EUR|http://www.esimerkki.fi/success|http://www.esimerkki.fi/cancel|http://www.esimerkki.fi/notify|5.1|fi_FI|0412345678|0412345678|esimerkki@esimerkki.fi|Matti|Meikäläinen||Testikatu 1|40500|Jyväskylä|FI|1|2|Tuote #101|101|1|10.00|22.00|0|1|Tuote #202|202|2|8.50|22.00|0|1";
String prehashIso = new String(prehash.getBytes("ISO-8859-1"),"ISO-8859-1");

String hash = Crypt.md5sum(prehash).toUpperCase(); 
String hashIso = Crypt.md5sum(prehashIso).toUpperCase();

Unfortunately, both hashes are the same as the value c83cf67455af10913d54252737f30e21 According to Suomen verkkomaksut's documentation, the correct value for this example case is 975816a41b9eb79b18b3b4526569640e

Is there any way to calculate MD5 hash in java with iso-8859-1 string?

Update: while waiting for Sumen verkkomaksut's answer, I found another way to make hash Michael Borgwardt corrected my understanding of strings and encoding, and I found a way to create hashes from byte []

Apache Commons is a good library source. I found that their digestutils class has an md5hex function, which accepts byte [] input and returns a 32 character hexadecimal string

For some reason, this is still invalid Both return the same value:

DigestUtils.md5Hex(prehash.getBytes());
DigestUtils.md5Hex(prehash.getBytes("ISO-8859-1"));

Solution

Java has a standard Java security. Messagedigest class, used to calculate different hash values

This is sample code

include java.security.MessageDigest;

// Exception handling not shown

String prehash = ...

final byte[] prehashBytes= prehash.getBytes( "iso-8859-1" );

System.out.println( prehash.length( ) );
System.out.println( prehashBytes.length );

final MessageDigest digester = MessageDigest.getInstance( "MD5" );

digester.update( prehashBytes );

final byte[] digest = digester.digest( );

final StringBuffer hexString = new StringBuffer();

for ( final byte b : digest ) {
    final int intByte = 0xFF & b;

    if ( intByte < 10 )
    {
        hexString.append( "0" );
    }

    hexString.append(
        Integer.toHexString( intByte )
    );
}

System.out.println( hexString.toString( ).toUpperCase( ) );

Unfortunately, it produces the same "c83cf67455af10913d54252737f30e21" hash value So, I think your crypto class is exempt I specially added prehash and prehashbytes length printouts to verify that 'iso-8859-1' is indeed used In this case, both are 328

When I did presh When GetBytes ("UTF-8"), it generates "9cc2e0d1d41e67be9c2ab4aabdb6fd3" (and the length of the byte array becomes 332) Again, it's not what you're looking for

So I guess Suomen verkkomaksut massaged some prehash strings they didn't record, or you ignored them

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>