wood burning stoves 2.0*
The moose likes Java in General and the fly likes how to send a byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to send a byte array" Watch "how to send a byte array" New topic
Author

how to send a byte array

Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
I have a byte array that is a Message digest. I don't want to convert it to string and pull back the array on the other end.

Can I send the byte array using output stream. If I do so, does it work in case of a post(if the jsp is invoked by a post).


Kishore
SCJP, blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60755
    
  65

When you submit a request, whether it be a post or a get, all request params must be strings.

Perhaps a little more info on what and why you are doing this might help.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
when I convert the byte array to string, i can access the value on the other end as a string. When I convert that back to byte array using string.getBytes() I am not able to replicate the byte array.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60755
    
  65

OK, that's a straight-forward Java issue, so I'm going to move this to the Java in General(intermediate) forum. I suggest you post your conversion code so we can figure out what is wrong with it. (Please use the UBB code tags to preserve the formatting).
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
Without seeing the conversion code first I would have to ask what type of encoding you are using when you do a string.getBytes() - is it the same as the encoding you started with?
Mattias Arthursson
Ranch Hand

Joined: Jul 26, 2004
Posts: 90
When you make strings out of bytes in Java a lot of strange character encoding issues happen behind the scenes. One suggestion to be able not to worry about this would be to Base64 Encode/Decode the data.

Unfortunately, to my knowledge, there's no Base64Encoder/Decoder included in the standard Java API, but if I'm not mistaken there's an implementation included in the JDK (1.4) in the sun.misc packages:

sun.misc.BASE64Encoder
sun.misc.BASE64Decoder

Otherwise there's an implementation on SourceForge:
http://iharder.sourceforge.net/base64/


Admit nothing. Blame everyone. Be bitter.
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
I begin with encoding with base64 like this:
raw = sha.digest(secureKey.getBytes("UTF-8"));

And then I create a string from 'raw' so that I can send using a post using raw.toString();

on the other end, I do the following:
byte[] actualSignature =(request.getParameter("secureMessage").trim()).getBytes("UTF-8");

I get totally different value from the byte array I started with.
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Mattias Arthursson:
When you make strings out of bytes in Java a lot of strange character encoding issues happen behind the scenes. One suggestion to be able not to worry about this would be to Base64 Encode/Decode the data.

Unfortunately, to my knowledge, there's no Base64Encoder/Decoder included in the standard Java API, but if I'm not mistaken there's an implementation included in the JDK (1.4) in the sun.misc packages:

sun.misc.BASE64Encoder
sun.misc.BASE64Decoder

Otherwise there's an implementation on SourceForge:
http://iharder.sourceforge.net/base64/


I know we can do Base64Encoding. The problem is, it creates a string that is always the same. To propagate across as a security key, if I sent the actual byte array, it looks different for a hacker each time(since for same key the message digest byte array looks different for each invokation)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
I know we can do Base64Encoding. The problem is, it creates a string that is always the same.

If Base64 encoding creates a string that is "always the same" then you MUST be starting with the same input every time.
Converting to and from strings with getBytes, etc is always going to be a disaster with random binary data because String conversion involve some assumptions about encoding. The sequence of envents:

byte[] -> base64encode -> (a string in ASCII chars) -> base64decode -> byte[]
is safe with respect to the string being transmitted, read, etc.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to send a byte array
 
Similar Threads
How to transport 2 dimension array of integer using byte array.
want to send pdf file to client from server. pdf is generated at server
CORBA help!!!!!!!
Converting image to byte array in j2me
Java image and byte[] for J2ME and J2SE