Create String representation of byte[] from MessageDigest.digest()

Hi all,

Probably a simple question, but I'm new to crypto. I need to get the String representation of an MD5 MessageDigest so I can pass it as a url parameter. Code snippet follows...

MessageDigest md5 = MessageDigest.getInstance("MD5");

md5.reset();

md5.update(id.getBytes());

md5.update(login.getBytes());

md5.update(usernum.getBytes());

md5.update(ypburl.getBytes());

md5.update(ma.getBytes());

md5.update(password.getBytes());

byte [] hash = md5.digest();

String myString =new String(hash);

myString ends up being a cryptic "byte-looking" value like "n@$?Y?U?6&?^"

How do I get the String representation of this, like "Yku2TILu0...." ?

-Derek

[859 byte] By [beattris] at [2007-9-26 5:21:09]
# 1
We use sun.misc.BASE64Encoder.encode(byte[]) to encode.And sun.misc.BASE64Decoder.decodeBuffer(java.lang.String) to decode such encryted strings that we need to send as URL parameters etc.
neville_sequeira at 2007-6-29 19:26:50 > top of Java-index,Security,Cryptography...
# 2

Many apps that need to send binary data over channels that are intended for printable ASCII text use something called "Printable" or "Base-64" Encoding. (RFC 1421, 4.3.2.4. See http://www.freesoft.org/CIE/RFC/1421/17.htm )

I don't have any source available, but the gist of the encoding algorithm is this:

1. for each 3 byte group,

1.1 aggregate them into a single 24-bit group,

1.2 chop that 24-bit value into 4 6-bit values,

1.3 lookup the character each 6-bit value maps to in a pre-defined table,

1.4. output those characters to the output stream or string.

For decoding, just reverse the process.

(note: there are a pair of classes in the sun.misc package distributed with the JDK, BASE64Encoder & BASE64Decoder, if you need a jump start).

DragonMan at 2007-6-29 19:26:50 > top of Java-index,Security,Cryptography...
# 3
Thanks, that's the answer I was looking for.-Derek
beattris at 2007-6-29 19:26:50 > top of Java-index,Security,Cryptography...
# 4

Note that when grouping bytes into 24 bit chunks, the input data may not be a multiple of 3 bytes.

So you may need to pad the input data with 1 or 2 additional null bytes before splitting it into founr 6-bits units each used as an index to the Base64 coding alphabet. The resulting String will then contain "too much" data.

However:

- if you have padded your input data with one additional null byte, the last 6-bit unit will always be 0, and will be converted by using the '=' encoding character instead of the first character of the Base64-alphabet. This equal sign is not absolutely required and can be stripped from the output if it is supported by the enveloppe format in which you'll insert that Base64 string. When decoding, you need to discard this equal sign.

- if you have padded your input data with two additional null bytes (16 bits), the last two 6-bit units will always be 0 and will each be converted into an '=' sign, which you can optionally strip from the output if you don't absolutely need it.

verdyp at 2007-6-29 19:26:50 > top of Java-index,Security,Cryptography...