Meaningless Drivel is fun!*
The moose likes Security and the fly likes Classcastexception - can not convert object into byte[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "Classcastexception - can not convert object into byte[]" Watch "Classcastexception - can not convert object into byte[]" New topic
Author

Classcastexception - can not convert object into byte[]

Raghu Sha
Ranch Hand

Joined: Feb 02, 2013
Posts: 118
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
Ranch Hand

Joined: Aug 27, 2012
Posts: 971
    
  10

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

Joined: Feb 02, 2013
Posts: 118
Encrypted the user id from form and store it into session
// Convert into byte[] and pass into decrypt method
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39571
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4242
    
    7

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

Joined: Feb 02, 2013
Posts: 118
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
Ranch Hand

Joined: Aug 27, 2012
Posts: 971
    
  10

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classcastexception - can not convert object into byte[]
 
Similar Threads
Basic use of JCE (Java Cryptography Extension)
RC4 Problem
JCE problem URGENT HELP Needed!!!
Length of Encrypted String Longer than Original String... Help
javax.crypto.BadPaddingException: Given final block not properly padded