I’m trying to port an app that uses Spongycastle over to iOS, but I’m getting this exception at runtime:
Caused by: java.security.spec.InvalidKeySpecException: Must use ECPrivateKeySpec or PKCS8EncodedKeySpec; was org.spongycastle.jce.spec.ECPrivateKeySpec
at com.android.org.conscrypt.OpenSSLECKeyFactory.engineGeneratePrivate(OpenSSLECKeyFactory.java:66)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:187)
... (and on)
Hi,
I investigated and found that it’s because Spongycastle uses ClassLoader.loadClass to load its algorithms, but I understand MOE has a known limitation with respect to loading classes dynamically from the classpath at runtime. Spongycastle couldn’t find everything it should have been able to, meaning the system was falling back on the default security provider from the standard library (and thus giving the above exception).
To resolve this, I created my own implementation of the BouncyCastleProvider that extends java.security.Provider and explicitly calls ‘new’ on the algorithm I wanted. I inserted this using the following code (which is the same code that is used to put SpongyCastle into the list of providers on Android devices):