• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Big Integer to String - weird characters

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a Simple RSA enabled chat application in java.

Message sent from Client A (or Client B) in encrypted form, the encrypted message is displayed on Client B (or Client A if sent from B) text area in encrypted form. User clicks decrypt button on client B then we will get the value from text area () then perform necessary cleaning then decrypt Problem: text2 is not in readable format (Look the attached Image ).

I have attached ChatClient.java ( where GUI and rsa code runs) and Chatserver( to run the server)



Chat Client





Code - which runs decrypt button is clicked
 
Ranch Hand
Posts: 30
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From my non programming experience, as I do not have any experience in java encryption, is that you might have a key mismatch. I haven't been on the programming side, but I have seen plenty of times where a key mismatch jumbles text/packets similar to what you have gotten. =/


**Edit:

This I believe is above beginning java, but they will likely be able to forward it to the proper thread.

**
 
Sheriff
Posts: 28360
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your test code is excessively complicated, in my opinion. When I replaced it by this:



I reliably got the same output as input using both methods, for a variety of inputs. I can't tell how your code works but I speculate that you are modifying the encrypted data in some way before decrypting it. For example in your attached image, the contents of the text field is the encrypted data preceded by some debugging text; if you try to decrypt that, you aren't going to get the original plaintext.
 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul

I understood the mistake I am making I.E

I am encrypting using RSA public key but while decrypting I using different key. Thats why different output.

Which brings me to different problem

Please take a look at the image

 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your methods


and


are the problem. The toString() method of a BigInteger cannot be reverted using the BigInteger(message) constructor and nothing in the Javadoc indicates it can be.

Your ciphertext is binary data and String is not a valid container for binary data unless the binary data is first encoded. For this simple example I suggest you took at Base64 encoding the ciphertext before sending it across your network.

P.S. This is a classic mistake made by many when they first start using encryption.

 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Richard - That makes sense

Can you explain second problem(in my second post)
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi Ram wrote:Thanks Richard - That makes sense

Can you explain second problem(in my second post)



Sorry but the writing on the image is too small for me to read.
 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for small image

So in my chat application (As you can see two GUI with same GUI components)

In Client A - user types text and clicks send button, which will display the encrypted text in Text Area in Client B.

This is done using following line



where pw is

There is decrypted button in my GUI, which when clicked would display the text in another text area named decryptedtext. I do this by

I have added listners to each button and in action performed i choose what to displayed based on which button clicked




Now problem is : When user clicks Decrypt button (in Client A button i.e where we click send button) it displays the text in textarea (decrypttext) but if i click same Decrypt button in Client B(where it displays the text sent from Client A) nothing is getting displayed. How to make it work in both windows

 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand the problem correctly but it seems to me you just have to send a message between the two to say 'decrypt' . If I do understand correctly then you will have to define a protocol that allows you to discriminate between the base64 encoded ciphertext and the 'decrypt' message. If I were doing this I would probably make it XML based but the approach to be used is up to you.
 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to edit my original post ?
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi Ram wrote:How to edit my original post ?


Why should you edit the original post? Doing do could make the replies that have already been made seem incorrect or confusing.

If you have new information, post it as reply to this topic.
 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I corrected my mistake in above code and made below changes but getting different error

Error"Not PKCS#1 block type 2 or Zero padding" exception "

Run ChatClient.java - Dialog box is shown where user enters username. Then private key and public key are generated and stored in C:/username/publickey,C:/username/privatekey.

Above step repeats when we run ChatClient.java again.

Then user1 sends message to User2 in encrypted form using User2 public key (Which is working fine)(after encryption , message is stored in text file) then User2 clicks decrypt button which decrypts the text (reads the encrypted text from file) using User 2 private key then display in textarea.

When I trying to decrypt I am getting "Not PKCS#1 block type 2 or Zero padding" exception

 
Paul Clapham
Sheriff
Posts: 28360
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit confused by this code:



First of all I'm confused about why you would use the "ready" method. But anyway, your goal for this code was to read the last line of text from the input and store it in the "test90" variable, right?
 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Paul thats my goal

Basically when i encrypt the text , i store it in file (cipher.txt) to later use it to decrypt it

also can you tell me how to store value in global variable which I can use later rather then in file
 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code when user clicks send button - I do encrypt and store in file

 
Paul Clapham
Sheriff
Posts: 28360
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Right. So not only do you throw away all of the cipher text except the last line when you read it, when you write it you're appending an unnecessary line-end character.

But really your cipher text isn't a series of lines anyway, is it? It's just a series of characters. So you should really stop treating it as lines. Don't use any methods which write lines or read lines, just write all the characters to the file and then read them all back.
 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@paul - It is not series of lines , just a series of characters.

ok , I will write characters to the file and then read them back

Thanks , will let you know
 
Abhi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Right. So not only do you throw away all of the cipher text except the last line when you read it, when you write it you're appending an unnecessary line-end character.

But really your cipher text isn't a series of lines anyway, is it? It's just a series of characters. So you should really stop treating it as lines. Don't use any methods which write lines or read lines, just write all the characters to the file and then read them all back.



if i use character stream, when i read from file I am getting it as Integer. Again I need to do conversion

Can you give example
 
please buy my thing and then I'll have more money:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic