Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java security class cast exception

Hi I am beginner in Java security, i encounter the following issue, when calling :

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

I got the error :

java.lang.ClassCastException: com.sun.crypto.provider.RSACipher cannot be cast to javax.crypto.CipherSpi

There are 5 more jars file for a payment gateway encryption in my war file:

  • cryptix-jce-api.jar
  • cryptix-jce-provider.jar
  • cryptix-message-api.jar
  • cryptix-openpgp-provider.jar
  • cryptix-pki-api.jar

Without these 5 JAR files then the Cipher.getInstance() works, but that is not possible because I need the JARs for payment information encryption.

Anyone out there can show me the path of how should I overcome this issue?

like image 329
Wilson60 Avatar asked Oct 16 '12 12:10

Wilson60


People also ask

How do you overcome class cast exception in Java?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What is class cast exception in Java?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

What causes a class cast exception?

A class cast exception is thrown by Java when you try to cast an Object of one data type to another. Java allows us to cast variables of one type to another as long as the casting happens between compatible data types.

What exception may be thrown during casting?

Downsides of Casting If you were to try to cast something like an Integer to a String, then you'll get a ClassCastException . This is what's known as a run-time exception , as it's really only detectable when your program is running.


2 Answers

Powermockito can't enhance javax.crypto classes so you might to add following annotation at the class level:

@PowerMockIgnore("javax.crypto.*") or @PowerMockIgnore({"javax.crypto" })

like image 99
Ami Avatar answered Oct 24 '22 09:10

Ami


Thanks for all the comments and answers.
In the end, I moved the 5 Jars out from WAR file to server Jars library directory and solved this.
In between there was an java.lang.RuntimeException: NYI.
It was solved using the solution from here: http://javabeanz.wordpress.com/2009/06/11/java-lang-runtimeexception-nyi/
Hope this helps, to anyone out there.
Once again thanks!

like image 1
Wilson60 Avatar answered Oct 24 '22 08:10

Wilson60