I do "sign" operation and have the following code:
KeyStore.PrivateKeyEntry privateKeyEntry =
(KeyStore.PrivateKeyEntry)keyStore.getEntry(keyAlias, null);
PrivateKey privateKey = privateKeyEntry.getPrivateKey();
signature = Signature.getInstance("NONEwithRSA");
signature.initSign(privateKey);
signature.update(data);
After executing the above, I get the following exception:
05-29 17:33:36.106 W/System.err( 4478): java.security.InvalidKeyException: Supplied key (android.security.keystore.AndroidKeyStoreRSAPrivateKey) is not a RSAPrivateKey instance
05-29 17:33:36.107 W/System.err( 4478): at org.spongycastle.jcajce.provider.asymmetric.rsa.DigestSignatureSpi.engineInitSign(DigestSignatureSpi.java:92)
05-29 17:33:36.107 W/System.err( 4478): at java.security.Signature$Delegate.init(Signature.java:1208)
05-29 17:33:36.107 W/System.err( 4478): at java.security.Signature$Delegate.chooseProvider(Signature.java:1167)
05-29 17:33:36.107 W/System.err( 4478): at java.security.Signature$Delegate.engineInitSign(Signature.java:1232)
05-29 17:33:36.107 W/System.err( 4478): at java.security.Signature.initSign(Signature.java:607)
05-29 17:33:36.107 W/System.err( 4478): at com.example.TestClass.sign(TestClass.java:289)
I also use the Spongy Castle library in the same class because I need it for other purposes, so therefore I have the following in a static block:
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
What is the problem here? I've read all the threads on StackOverflow that are of similar nature and the solution is always to not use a specific provider when doing init on the Cipher (which I do not) and to not cast the key to RSAPrivateKey (which I do not!).
Does it have something to do with Spongy being registered as a 1st provider? I do not have any ideas left. Please share