• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to send a byte array

 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic