Solved

unable to store encrypted characters through servlet

Posted on 2001-06-06
8
243 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:Arod
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6162047
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
 
LVL 2

Expert Comment

by:dhymes
ID: 6162193
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
 

Author Comment

by:Arod
ID: 6162240
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
 
LVL 2

Expert Comment

by:dhymes
ID: 6162342
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Arod
ID: 6162411
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
 
LVL 3

Expert Comment

by:chrisos
ID: 6163117
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
 

Author Comment

by:Arod
ID: 6163928
If you can do that Chrisos I would appreciate it!
0
 
LVL 3

Accepted Solution

by:
chrisos earned 200 total points
ID: 6164058
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now