• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

how to send a byte array

 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic