This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Cryptography - deciphering doesn't yield the same text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Cryptography - deciphering doesn Watch "Cryptography - deciphering doesn New topic
Author

Cryptography - deciphering doesn't yield the same text

Franc Dijan
Greenhorn

Joined: Jun 01, 2011
Posts: 2
Hi,

I'm trying to implement cyptography and found this code sample from Java SE documentation.
However, when I encrypt a text and then decrypt it again, the end result is not the same as the original text.
Could you tell me what's wrong?

Thanks!



Result:
cleartext : [B@dd20f6
ciphertext : [B@723d7c
cleartext1 : [B@22c95b
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Franc Dijan wrote:
cleartext : [B@dd20f6
ciphertext : [B@723d7c
cleartext1 : [B@22c95b


You are not printing the values of the clear and cipher text array. You are printing the value returned by the toString() method for the byte array -- which is just "[B", which means byte array, followed by the identity hash code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Also, for arrays .equals returns only if the two references are the same (in other words, ==). Use assertArrayEquals to test if they are the same:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Franc Dijan
Greenhorn

Joined: Jun 01, 2011
Posts: 2
You two are absolutely right. I've changed the comparison & print method & the two text were the same all along.
Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cryptography - deciphering doesn't yield the same text
 
Similar Threads
Using a one way hash for Password encryption
Help please: Encrypt, save to file, then decrypt later to read
3DES encryption and decryption
Encrypt data before storing database
Help please: Encrypt, save to file, then decrypt later to read