MD5 and international characters

Jonas Nilsson (jonas.nilsson@redalen.se)
Thu, 5 Aug 1999 17:17:22 +0200

From: Jonas Nilsson <jonas.nilsson@redalen.se>
To: "'java-security@java.sun.com'" <java-security@java.sun.com>
Subject: MD5 and international characters
Date: Thu, 5 Aug 1999 17:17:22 +0200

Hi,

I wonder if you could give me some hints on a problem (sorry if you've =
got
this already, because I've also reported this as a bug):

I've written a program that takes a String, uses MessageDigest with =
MD5, and
prints out the 32-character string with BigInteger. I then compare
the message digest with another one ("incoming MAC") that has been =
generated
on another web. This works fine, as long as it is US-ASCII only,
but when I have the swedish characters =C5=C4=D6, it gives a different =
message
digest than the incoming MAC.
=20
I've tried to use all different encodings there is - ISO8859_1, =
Unicode, ...
but nothing gives the correct MD5. Have you succesfully done MD5 with
international characters in Java?
=20
Here is part of my program (without any encoding):
=20
static String MD5 (String arg) {
try {
// create a message digest class with MD5
MessageDigest md5 =3D MessageDigest.getInstance("MD5");
=20
// update the digest with the string to be
// fingerprinted/message digested
md5.update (arg.getBytes ());
=20
// do the actual digest
byte[] digest =3D md5.digest ();
// BImax is 1 followed by thirtytwo zero's in hex
BigInteger BImax =3D new BigInteger =
("100000000000000000000000000000000",
16);
=20
BigInteger BIdigest =3D new BigInteger(digest);
if (BIdigest.signum () =3D=3D -1) { // if the digest is negative...
// ... take BImax and add, to get the positive equivalent
BIdigest =3D BIdigest.add (BImax);
}
// the message digest is then printed as hex - between 1 and
// 32 characters long
String padded =3D BIdigest.toString (16);
while (padded.length () < 32) {
padded =3D "0" + padded;
}
return padded;
} catch (Exception e) {
System.err.println (e);
return "(error)";
}
}
=20
I've looked around, asked people I know, but I haven't been able to =
come up
with anything... Thanks in advance for any help/suggestions!
=20
Best regards, Jonas