• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not decrypting properly  RSS feed

 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have encrypted a String with a secret key and encrypted the secret key with the public key and I am now decrypting. The code is running fine and I am running into no errors but it is not decrypting correctly. I am not getting the same key once decrypted since it is just giving me characters and my decrypted text is not the same either as it is also characters.


Here is my encryption and decryption code

 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind I am not decrypting with my private key like I need to which is the problem
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither one of your decrypting methods do any ... well ... decrypting...

[EDIT] response 30 seconds too late...

Henry
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although I can't figure out the syntax for using Base 64 to decrypt the secret key
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I use Base64 for decoding, I keep getting errors when I try to do the opposite of how I encrypted
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:Although I can't figure out the syntax for using Base 64 to decrypt the secret key


Base64 is *not* an encryption algorithm. It is an encoding algorithm, which is used to encode binary data into a text string. The idea is... with a text string, you can store it in a text file, or anywhere else, easier than you can with binary data.

Henry
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I think I should be decrypting (not sure though)





I am getting this error though:

 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I fix this error? Is there a better way to decode than what I did?
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you're working with Strings in the first place. Why don't you just work with byte arrays?

Also, why are you not using Cipher.wrap() and Cipher.unwrap() to encrypt/decrypt your symmetric key?
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I don't understand why you're working with Strings in the first place. Why don't you just work with byte arrays?

Also, why are you not using Cipher.wrap() and Cipher.unwrap() to encrypt/decrypt your symmetric key?



Well I am pretty new at all of this encryption and decryption so it would be great if you would actually help with what I have done instead of diminishing it completely.
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've given you pointers in all your previous threads and you've ignored them, so now I'm asking if you have good reasons to ignore them.

As Henry has pointed out before, you need to reverse your process. That doesn't just mean reverse the operations you perform, but also reverse the order of the operations. For instance, after you've encrypted your plaintext to a ciphertext and then encoded your ciphertext as base64, you can't decrypt the String before you decode it, because you need to decrypt the raw ciphertext, not the base64.
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
As Henry has pointed out before, you need to reverse your process. That doesn't just mean reverse the operations you perform, but also reverse the order of the operations.


As another example, if you take a letter and seal it in an envelope; then take the envelope and lock it in a box; and then take that box and lock it in a safe.... the first step to undo everything, would not be to unseal the envelope...

Henry
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was finally able to decrypt the key like this:




I am now going to decrypt the encrypted String with this decrypted key. Since the decrypted key is in the format String , I need to convert it back into a Key so this is what I did:




I am getting this error though and I am not sure how to fix it:

 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to change the code for the decryptedTextHex:



Realized I made a mistake but it still gives me the same error.
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:So I was finally able to decrypt the key like this:

Your decryptedKey() method... well ... doesn't do any decrypting at all. From the looks of it, it is doing some XML parsing?

Why are you doing that? Totally confused...

Henry
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Your decryptedKey() method... well ... doesn't do any decrypting at all. From the looks of it, it is doing some XML parsing?
Why are you doing that? Totally confused...



I am not sure, when my method was not working I googled some more and a lot of posts were saying to convert the byte to a String text which is what I did and I wasn't getting any errors from the code I posted.
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I am pretty much stuck and I am not sure how to go forward
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's wrong. Strings have nothing to do with keys. For now, stop using strings. Stop using encodings. Your very first task is to encrypt a given message using a given key, and then to decrypt a message using a given key. Here are the method signatures. Show me how you would implement these, and then we'll move on to the rest of the tasks. You are NOT allowed to change the values of the constants I defined, and your are NOT allowed to change the method signatures and return types.
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I only partially agree with Stephan here...

On the agreement part ... The encryption side, as implemented, is convoluted and messy. So, the decryption side is, at best, going to be the same. Perhaps, it is better to start over. To work on the encryption/decryption portion only. To work on both together, and confirms that it works.... and later, once that part works, then add any encoding, which again, to be done together.

On the disagreement part... The encryption side is mostly done.  And the decryption side, from earlier posts, is in much better shape. Additionally, Stephan and I had provided many hints, that for the most part has been ignored.  Perhaps, it is better to revisit all the old posts, from this topic and earlier topics. Start from older code. Look at all the previous hints, and make an attempt from there.

Jerry Girgich wrote:I think I am pretty much stuck and I am not sure how to go forward


To me, either option mentioned would work at getting you forward.  On the other hand, just guessing via code snippets from Google, probably won't work. As you noticed, this can actually get you moving backwards...

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!