4

When I'm using this KeyPairGeneratorSpec object in API 24, my class works.

KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(this)
                    .setAlias(KEY_ALIAS)
                    .setSubject(new X500Principal("CN=" + KEY_ALIAS))
                    .setSerialNumber(BigInteger.TEN)
                    .setStartDate(start.getTime())
                    .setEndDate(end.getTime())
                    .build();
            KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
            kpg.initialize(spec);
            keyPair = kpg.generateKeyPair();

When compiling on lower than 24, I get this error:

java.security.InvalidAlgorithmParameterException: Only RSAKeyGenParameterSpec supported

I don't understand, as KeyPairGeneratorSpec should be available from api 18?

Greg
  • 689
  • 2
  • 8
  • 23
  • Same problem here, did you find an (elegant) way to fix this? – Xvolks Jan 18 '17 at 10:17
  • 2
    @Xvolks I don't remember well how I fixed it, can you try adding this parameter to the getInstance method? : KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA, ANDROID_KEYSTORE);private static final String ANDROID_KEYSTORE = "AndroidKeyStore"; – Greg Jan 18 '17 at 13:13

1 Answers1

4

Had this problem earlier today as well with the Pixel emulator (API 23) raising an "Only RSAKeyGenParameterSpec supported" exception, while a Nexus 5X (API 24) was fine.

Can confirm that the fix was to add 'AndroidKeyStore' as the second parameter to the call to getInstance on KeyPairGenerator as @estoke mentioned above:

ie:

KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");

crafterm
  • 1,831
  • 19
  • 17
  • I wonder why the same error happens to me on an API 29 emulator. It makes no sense to implement a solution that would be designed for API 23 and lower... Not sure what's going on. – Maximiliano Ambrosini Dec 10 '20 at 18:45