Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I am trying to generate an Elliptic Curve private key but I keep on getting this error on android OS versions 5 and 6. Below is the code to generate the PrivateKey with EC cryptography.
private static java.security.PrivateKey createPrivateKey(List<PrivateKey> privateKeys, String appInstallationIdentifier) throws Exception {
PrivateKey privateKey = getPrivateKeyForActivation(privateKeys);
String key = decryptMessage(privateKey.getEncryptedPrivateKey(), appInstallationIdentifier, privateKey.getIv());
key = key.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s", "");
byte[] privateKeyFileContent = decode(key);
KeyFactory keyFactory = KeyFactory.getInstance("EC");
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(privateKeyFileContent);
return keyFactory.generatePrivate(ks);
And here is the error that I receive on OS versions 5 and 6
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0f06707b:elliptic curve routines:EC_GROUP_new_by_curve_name:UNKNOWN_GROUP
Any ideas why I receive this errors? also EC is supposed to be supported from API level 11 and above so using EC should not be a problem.
You have to be careful when presuming which crypto algorithms are supported on a device and from which version.
Some vendors even place their own versions of security providers making even more troubles.
I would recommend trying out installing your own security provider called SpongyCastle and if you believe that the EC should be supported on those phones you can check the available algorithms like suggested here:
http://mobile.developer.com/ws/android/encrypting-with-android-cryptography-api.html
I would like to add something to originx's answer.
I had the exact same error while trying to use
KeyAgreement.getInstance("ECDH);
What happened to me was that I was adding SpongyCastle
as a SecurityProvider
, doing this:
Security.addProvider(new BouncyCastleProvider());
And for some devices everything worked as expected but in others some device (namely LGE Nexus 5) I got the exact same exception.
What fixed it for me was changing the way I add SpongyCastle and doing:
Security.insertProviderAt(new BouncyCastleProvider(), 1);
Hope this helps!
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.