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:
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?
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.
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.
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.
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.
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" })
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With