Solved

unable to store encrypted characters through servlet

Posted on 2001-06-06
8
247 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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
 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

829 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