Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cryptography - deciphering doesn't yield the same text

 
Franc Dijan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Franc Dijan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You two are absolutely right. I've changed the comparison & print method & the two text were the same all along.
Thanks!
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic