aspose file tools*
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
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: 19070
    
  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: 19785
    
  20

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: 19785
    
  20

You're welcome.
 
Don't get me started about those stupid light bulbs.
 
subject: Cryptography - deciphering doesn't yield the same text