How to byte[ ] --> char[ ] and char[ ] --> byte[ ]?

R

Richard

Hello,

I get a char[] from a JPasswordField and I would like to use
MessageDigest.digest(byte[]) to get a md5 digest of the password. What's
the best way to transform a char[] into a byte[] in Java (and to
transform a byte[] into a char[])?

I don't want to use a String to do that.

Thanks in advance for your answers.

Richard
 
J

Jeroen V.

This is a way, don't know whether it's the best (guess you mean most
performant) way...

char[] chars = "azertyuiop".toCharArray();
byte[] bytes = new byte[10];
int i = 0;
for(char c : chars) bytes[i++] = (byte)c;
i = 0;
for(byte b : bytes) chars[i++] = (char)b;
System.out.println(chars);
 
R

Richard

OK, I know that, thanks. My problem is to get a md5 digest of a password
registered in a char[]. This problem seems simple but can't find a
simple solution to do it. Does it exist one? Else, do you know a not so
simple one?

Another question: does a md5 digest can be registered in a String? Or
does it contains bytes that can't be registered in a String?

Cheers,

Richard

(e-mail address removed) opalinski from opalpaweb a écrit :
 
V

Venky

I don't want to use a String to do that.

Any special reason why you don't want to use String to convert char[]
to bytes[].
Something like this.
MessageDigest.digest((new String(char[])).getBytes());
 
A

Adam Maass

Richard said:
I get a char[] from a JPasswordField and I would like to use
MessageDigest.digest(byte[]) to get a md5 digest of the password. What's
the best way to transform a char[] into a byte[] in Java (and to transform
a byte[] into a char[])?

You need to decide which /encoding/ you are going to use to do the
transformation from char[] to byte[] (and back again). char is 16-bits; byte
is 8-bits; there are any number of ways to map the 16-bit values onto 8-bit
values.

One such encoding is ASCII (though based on the .fr domain in your email,
probably not a good choice). Then the code to get a byte[] from a char[] is
very simple:

byte[] bytes = new String(chars).getBytes("ASCII");

There is also the not-quite analogous getChars method...

String str = new String(bytes, "ASCII");
char[] chars = new char[str.length()];
str.getChars(0, chars.length, chars, chars.length);
I don't want to use a String to do that.

The String class has the very handy getBytes() and getChars() methods, so
you probably want to take advantage of that. Or you could look at the source
for those methods and replicate it...



-- Adam Maass
 
A

Andrey Kuznetsov

Any special reason why you don't want to use String to convert char[]
to bytes[].
Something like this.
MessageDigest.digest((new String(char[])).getBytes());

probably same reason why JPasswordField has deprecated method String
getText() - it is unsecure

BTW the simplest method to convert chars to bytes and vise versa
is to use CharToByteConverter and ByteToCharConverter.
 
A

Andrey Kuznetsov

The String class has the very handy getBytes() and getChars() methods, so
you probably want to take advantage of that. Or you could look at the
source for those methods and replicate it...

till java 1.4 they used CharToByteCoverter and ByteToCharConverter.
 
R

Roedy Green

BTW the simplest method to convert chars to bytes and vise versa
is to use CharToByteConverter and ByteToCharConverter.

If you look at the String methods you might be horrified to see how
much copying and creating of intermediate objects goes on during
byte[] <=> String conversion.. These new methods can avoid much of
that overhead.
 
A

Adam Maass

Roedy Green said:
BTW the simplest method to convert chars to bytes and vise versa
is to use CharToByteConverter and ByteToCharConverter.

If you look at the String methods you might be horrified to see how
much copying and creating of intermediate objects goes on during
byte[] <=> String conversion.. These new methods can avoid much of
that overhead.

byte[] <=> char[] conversion is hard to generalize, and yet String presents
a pretty simple, general interface to the process. And String has that darn
immutible property to maintain, so I would expect that the conversion
routines in String to be less than totally efficient.

-- Adam Maass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top