File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Characters jumbled pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Characters jumbled pls help" Watch "Characters jumbled pls help" New topic

Characters jumbled pls help

Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
I am facing a very critical problem.....i am into cryptography and using DES algorithm to encrypt and decrypt a plain text.....
The main problem lies when i encrypt a particular plain text we get a set of weird characters being generated now when i try to send these characters through a socket then at the other end the reading is not interpreted this i mean that all characters in the ascii range 148-160 have a representation of a '?')(try printing on the console) and when any character(generated after encrypting) of this range is read at the other end it is read as a character with ascii value 63 which corresponds to real '?'.Why does this happen....
Arnaud Burlet
Ranch Hand

Joined: Oct 08, 2004
Posts: 31
I'm not sure why it happens, depends on how you send and receive the encrypted stuff, sorry.

But I'm sure you should not encrypt/send/decrypt char[], you should encrypt/send/decrypt byte[].

byte[] encrypted = encrypt(yourString.getBytes("UTF-8"));
byte[] toDecrypt;
new String(decrypt(toDecrypt), "UTF-8");
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

It appears that you had a valid display name and have changed it so it is no longer valid. Please change it back immediately, since accounts with invalid display names get deleted, often without warning.

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

I infer that you are encrypting some bytes and then converting them to a String using something like

byte[] encryptedBytes = cipher.doFinal(planeTextBytes);
String encrypted = new String(encryptedBytes);

This is almost certainly doomed to failure because not all byte combinations convert (reversibly) to a String. If you realy do need to send ASCII character then your best bet is to Base64 or Hex encode the encrypedBytes.

I try not to convert encrypted bytes or hash bytes to a String. The only time I have needed to is when I had to put hash bytes into a DB table where I was not allowed (customer forbade it) to to change the table structure in any way.

The following is my standard demonstration of DES encryption and shows an example of the use of Base64 to encode encrypted data.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi All
Thanks for your response ....but someone said that i should convert the characters to byte from and then send it ...but what happens is that characters above the ascii value 127 does not gets stored in a byte(as byte ranges from -128 to 127) and thus these values are stored wrongly .So i went in for writer class which uses characters rather then having stream classes and thus faced that problem.......But i basically dont understand how are characters interpreted wrongly...You all can try sending characters within ascii range 148-160 and receive them at the other end...They are interpreted wrongly(read as character with ascii 63).
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


As David has already asked you (see above), please change your display name to comply with our policy. You need a real-sounding first and last name. Don't post again before making the change. Thank you.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Characters jumbled pls help
It's not a secret anymore!