MD5 Hash String ISO-8859-1 in Java

I am implementing an interface for a digital payment service called Suomen Verkkomaksut . Payment information is sent to them via the HTML form. To ensure that no one interferes with the information during the transfer, the MD5 hash is calculated at both ends by a special key that is not sent to them.

My problem is that for some reason they seem to have decided that the incoming data is encoded using ISO-8859-1, and not UTF-8. The hash that I sent them is computed using UTF-8 strings, so it is different from the hash that they compute.

I tried this with 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 identical to the value of C83CF67455AF10913D54252737F30E21. The correct value for this example is 975816A41B9EB79B18B3B4526569640E according to the documentation of Suomen Verkkomaksut.

Is there a way to calculate the MD5 hash in Java with the lines of ISO-8859-1?

UPDATE: While waiting for a response from Suomen Verkkomaksut, I found an alternative way to make a hash. Michael Borgwardt corrected my understanding of strings and encodings, and I was looking for a way to make a hash from bytes [].

Apache Commons is a great source for libraries, and I found their DigestUtils class, which has an md5hex function that takes a byte input [] and returns a 32 character hexadecimal string.

For some reason this still doesn't work. Both of them return the same value:

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

Java java.security.MessageDigest .

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( ) );

"C83CF67455AF10913D54252737F30E21" . , , Crypto . prehash prehashBytes, , "ISO-8859-1". 328.

presash.getBytes( "utf-8" ), "9CC2E0D1D41E67BE9C2AB4AABDB6FD3" ( 332). , , .

, , Suomen Verkkomaksut prehash, , .

+2

, , , API- Crypt .

"" - - , .

Java UTF-16, , MD5 , . Crypt.md5sum() , , - ? , .

, , :

String prehashIso = new String(prehash.getBytes("ISO-8859-1"), "ISO-8859-1");

, ISO-8859-1 .

+8

, , ISO-8859-1 nordic ä ö SHA-256 . :

import java.security.MessageDigest;
//imports omitted

@Test
public void test() throws ProcessingException{
String test = "iamastringwithäöchars";           
System.out.println(this.digest(test));      
}

public String digest(String data) throws ProcessingException {
    MessageDigest hash = null;

    try{
        hash = MessageDigest.getInstance("SHA-256");
    }
    catch(Throwable throwable){
        throw new ProcessingException(throwable);
    }
    byte[] digested = null;
    try {
        digested = hash.digest(data.getBytes("ISO-8859-1"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    String ret = BinaryUtils.BinToHexString(digested);
    return ret;
}

, , Hex apache, .

+2

UTF-8, ISO-8859-1, . ISO-8859-1, Suomen Verkkomaksut, UTF-8. HTTP- , chasset = utf-8 Content-Type HTTP-.

One way to eliminate some problems is to try the String prefix, which contains only characters that are encoded the same in UTF-8 and ISO-8859-1. From what I see, you can achieve this by deleting all the “ä” characters in the string that you used.

+1
source

Source: https://habr.com/ru/post/1724604/


All Articles