aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Characters jumbled pls help" Watch "Characters jumbled pls help" New topic
Author

Characters jumbled pls help

Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi
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 properly....by 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"));
send(encrypted);
byte[] toDecrypt;
receive(toDecrypt)
new String(decrypt(toDecrypt), "UTF-8");
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"archies",
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.

Thanks,
David.
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
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Archies,

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: http://aspose.com/file-tools
 
subject: Characters jumbled pls help
 
Similar Threads
Byte streams and the super keyword
IO Streams and Characters.
Hanlde control characters in Servlet
int to char
doubt on char assignment