Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Richard - 30 Jan 2006 16:37 GMT
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
Jeroen V. - 30 Jan 2006 19:28 GMT
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);
opalpa@gmail.com opalinski from opalpaweb - 30 Jan 2006 20:15 GMT
A thing to keep in mind is are you sure what encoding your characters
are going to be?  char is 16 bit.  byte is 8 bit. (in java)

Cheers

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Richard - 30 Jan 2006 20:31 GMT
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

opalpa@gmail.com opalinski from opalpaweb a écrit :
> A thing to keep in mind is are you sure what encoding your characters
> are going to be?  char is 16 bit.  byte is 8 bit. (in java)
[quoted text clipped - 4 lines]
> opalpa@gmail.com
> http://www.geocities.com/opalpaweb/
Roedy Green - 30 Jan 2006 21:36 GMT
>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?

md5 and sha1 give you a byte[] of what appear at first to be random
bytes.  If you to transport that in printable format, you would need
some form of armouring.

See http://mindprod.com/jgloss/armouring.html
http://mindprod.com/jgloss/md5.html
http://mindprod.com/jgloss/sha1.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Venky - 31 Jan 2006 03:57 GMT
> 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());
Andrey Kuznetsov - 31 Jan 2006 09:46 GMT
> 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.

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Roedy Green - 31 Jan 2006 12:01 GMT
On Tue, 31 Jan 2006 10:46:59 +0100, "Andrey Kuznetsov"
<spam0@imagero.com.invalid> wrote, quoted or indirectly quoted someone
who 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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Adam Maass - 01 Feb 2006 07:34 GMT
> On Tue, 31 Jan 2006 10:46:59 +0100, "Andrey Kuznetsov"
> <spam0@imagero.com.invalid> wrote, quoted or indirectly quoted someone
[quoted text clipped - 7 lines]
> 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
Roedy Green - 30 Jan 2006 21:26 GMT
>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[])?

see http://mindprod.com/jgloss/conversion.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Adam Maass - 31 Jan 2006 06:56 GMT
> 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
Andrey Kuznetsov - 31 Jan 2006 10:16 GMT
> 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.

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.