unable to store encrypted characters through servlet

Hi.
 I have a question and requires immediate attention. The problem is the following.
    I have a palm application that I have set up to send a http post request using the INetLib API from palm. Now the information that I am posting are encrypted characters that get encrypted on the client side. However, when we view this data in the servlet we have generated to insert this data in the sql table it doesn't seem to recognize some of these encrypted characters. Some of the characters that the servlet doesn't recognize are shown as ? character.
      One of the reasons this may be happeing is because java I believe uses 16 bit unicode character set. Now the encryption that occurs use 64 bit encryption. Does anyone know if this may be causing the problem. And if it isn't what needs to be done to rectify the problem? Thanks for your help.
ArodAsked:
Who is Participating?
 
chrisosConnect With a Mentor Commented:
Arod,

Here goes:


To encode:
////////////////////////////////////////////////

// Convert the byte array into one big string
byte[] bytes = // YOUR DATA IN A BYTE ARRAY
Object send = null;
String asciiTemp = null;
int asciiTempLength = 0;
StringBuffer hexEncoded = new StringBuffer();
try {
     for( int byteCounter = 0; byteCounter < bytes.length; byteCounter++ ) {
          asciiTemp = Integer.toHexString((int)bytes[byteCounter]);
          asciiTempLength = asciiTemp.length();
          if( asciiTempLength > 2 ) {
               asciiTemp = asciiTemp.substring(asciiTemp.length()-2);
          }
          else if( asciiTempLength == 1 ) {
               asciiTemp = "0"+asciiTemp;
          }
          hexEncoded.append(asciiTemp+":");
     }
}
catch( Exception e ) {
     e.printStackTrace();
}
contentType = "text/plain";
send = hexEncoded.toString();

////////////////////////////////////////////////

To transmit from the servlet:

////////////////////////////////////////////////

// Write a text stream
System.out.println("Writing output");
OutputStream outStream = response.getOutputStream();
PrintWriter out = new PrintWriter(outStream);
out.write((String)send);
out.flush();
outStream.flush();
out.close();
outStream.close();

////////////////////////////////////////////////

To decode back into a byte array

////////////////////////////////////////////////

URL request = new URL("http...YOUR URL");
URLConnection connection = request.openConnection();

// Get the response
connection.setDoInput(true);
InputStream inStream = connection.getInputStream();
InputStreamReader inr = new InputStreamReader(inStream);
BufferedReader br = new BufferedReader(inr);
int size = 0;
String data = br.readLine();
br.close();
inr.close();
inStream.close();

// Reconstruct the byte array from the string
StringTokenizer byteTokenizer = new StringTokenizer(data, ":");
bytes = new byte[byteTokenizer.countTokens()];
int byteCount = 0;
while( byteTokenizer.hasMoreTokens() ) {
     //bytes[byteCount] = Byte.parseByte(byteTokenizer.nextToken());
     bytes[byteCount] = (byte)Integer.parseInt(byteTokenizer.nextToken(), 16);
     byteCount++;
}
System.out.println("Data retrieved: "+byteCount+" bytes");
System.out.println("Request = "+newURL);
URL request = new URL(newURL);
URLConnection connection = request.openConnection();

////////////////////////////////////////////////
0
 
Igor BazarnyCommented:
Hmm,

Can you post a bit of code? I believe that you problem is that you don't need characters at all, you simply need bytes, and attempt to read characters leads to conversion from bytes to unicode chars. Or maybe you have different encodings on client end server side.

Regards,
Igor Bazarny
Brainbench MVP for Java 1
www.brainbench.com
0
 
dhymesCommented:
Those encrypted characters need to be of a specific type prior to sending them accross the wire since it is binary data. What are you using to pass the data over the wire?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ArodAuthor Commented:
All that I'm doing is sending an Http post request. The purpose of the post is to assign a variable the value of the encrypted message. The encrypted message is encoded with a lot of funny characters due to the 64 bit encoding. The variable containing the encrypted message is sent to the servlet which makes a jdbc connection to a sql database. This is so that we can store the encrypted message in a table. The variable with the encryption message is retrieved using the req.getParameter() function and assigned to a string.
 The palm code was written in c. We are encrypting the message using the DES algorithm with mode OFB. Let me know if this helps.
0
 
dhymesCommented:
Those encrypted characters need to be of a specific type prior to sending them accross the wire since it is binary data. What are you using to pass the data over the wire?
0
 
ArodAuthor Commented:
What do you mean by the type of the encrypted characters? The variable in which the encrypted characters are stored is a pointer to character which is basically a string. This is how it is sent through the http post!
0
 
chrisosCommented:
I have come accross a similar problem over the last few days.  I had to transmit class files along a stream from a servlet to an application.  

The way I dealt with it was to convert the byte array into hex characters and transmit these characters (0-f), it isn't plesant!  I know it is not an elegent solution as you need to encode, transmit then decode and the transimssion duration will be increased due to the crap encoding!

If you are interesed I can post the code for you.

Regards,

Chrisos.
0
 
ArodAuthor Commented:
If you can do that Chrisos I would appreciate it!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.