I am developing an app using the Android KeyStore
API calls to encrypt some string values. I am targeting API level 18 and upwards. I have a an if else block in my code which targets API levels 18 up to 23. My problem is that I either have to use deprecated method calls in the block with (shown below is my if block)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
kpg.initialize(new KeyPairGeneratorSpec.Builder(alias, keyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
.setDigests(KeyProperties.DIGEST_SHA256,KeyProperties.DIGEST_SHA512)
.build());
KeyPair kp = kpg.generateKeyPair();
}
this appears with a line through it in the IDE as the method call is deprecated. But, it does target the api levels this code was built to run on?
If I change over to KeyGenParameterSpec.builder()
I get a compile time error because this object can only be used on API level 23 and above. My app targets API level 18 and above?
Is there a way to generate a key pair entry in my KeyStore
without using the deprecated method calls or having to move to API level 23 and above?
Thanks in advance!
Just include an else
block with the new API level 23 code so you can do it both ways!
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
// deprecated code on 23
}else{
// new code for 23+
}