ardevd
ardevd

Reputation: 3407

Generate RSA Keypair in the KeyStore on API level 21

So the KeyGenParameterSpec was introduced in API level 23 so on lower API levels I need to use the KeyPairGeneratorSpec. However, when setting the type I get an error with Android Studio saying the type must be (in this case) KeyProperties.KEY_ALGORITHM_RSA. However that constant was introduced in API level 23 so it wont work for API level 22 for example!

I've tried just replacing it with the constant value which is "RSA" but it wont accept it.

Any idea how I get around this?

KeypairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
    .setKeyType("RSA")
    .build();

So in the above simplified example, I set the keytype to "RSA" but Android Studio wont compile and forces me to use KeyProperties.KEY_ALGORITHM_RSA which again, is only available in API level 23 and above.

Upvotes: 1

Views: 2480

Answers (2)

Ankit_ceo2
Ankit_ceo2

Reputation: 317

The error that you are seeing are the inspection rules output. I also got the same inspection error but build is working fine and running as expected.

If you want to remove the inspection error, you can edit the inspection rules. However this is not required.

Upvotes: 0

Kuffs
Kuffs

Reputation: 35661

Taken from:

Android RSA Keypair Generation - Should I use Standard Java/Bouncy Castle/Spongy Castle/JSch/Other?

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair keyPair = kpg.genKeyPair();

Upvotes: 1

Related Questions