Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Characters jumbled pls help

 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic