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

Classcastexception - can not convert object into byte[]

 
Raghu Sha
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I stored one object in session.
get those value from session and then pass it into below method.
There is no issue in private key, getting classcastexception as cannot convert into byte[]

[Added code tags - see UseCodeTags for details]
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem has nothing to do with the decryption code you have posted. As the exception indicates, the problem is that you are trying to caste an object to a byte array but it is not a byte array. To give us a chance of working out what is wrong we would need to see how you insert the object into the session and how you extract it from the session.
 
Raghu Sha
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Encrypted the user id from form and store it into session
// Convert into byte[] and pass into decrypt method
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you go - if you store a String in a session, then that's what you need to retrieve, not a byte[].

By the way, encrypted data is binary, not text - so you can't store it in a String, at least not without first encoding it so that it becomes text. But since can just put the byte[] in the session, that should not be necessary anyway.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting isn't the same as conversion*. It's just telling the compiler to treat it as a different type - you're saying "I know you think it's that type, but trust me, it's really this type". If it isn't then you'll get an error at run time. It doesn't change the actual object at all. If you want to convert between byte arrays and Strings, there are methods and constructors in the String class that can do that.

* It's a bit different with primitive types. But arrays and Strings aren't primitive types.
 
Raghu Sha
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @ Ulf Dittmer,
I have to store encrypted data only in session.
Latter point of time, decrypt the data available in session and then verify.

Could you please suggest the best way ?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghu Sha wrote:Thanks @ Ulf Dittmer,
I have to store encrypted data only in session.
Latter point of time, decrypt the data available in session and then verify.

Could you please suggest the best way ?


Ulf has already indicated how to do it. Just put the ciphertext byte array directly in the session without converting to a String!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic