Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Android KeyStore API with API 18?

How do I get the equivalent code below when I'm targeting API 18? Code below works only for API 23 and above. Also how secure would the API 18 code be, given that we can't use KeyGenParameterSpec and the API 18 code might use deprecated APIs?

KeyGenerator keyGenerator = KeyGenerator.getInstance(
    KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");

keyGenerator.init(new KeyGenParameterSpec.Builder(alias,
    KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
    .setKeySize(256)
    .setUserAuthenticationRequired(true)
    .setUserAuthenticationValidityDurationSeconds(400)
    .setRandomizedEncryptionRequired(false)
    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
    .build());

SecretKey key = keyGenerator.generateKey();
like image 496
user299648 Avatar asked Aug 23 '16 18:08

user299648


1 Answers

Symmetric key generation and storage in the Android KeyStore is supported from Android 6.0 (API Level 23) onwards.

Asymmetric key generation and storage in the Android KeyStore is supported from Android 4.3 (API Level 18) onwards.

See this document for more info: Android Keystore System

Though there are some problems you can use Asymmetric key generation. Follow the reference bellow..

Asymmetric Key Generation

like image 180
Anisuzzaman Babla Avatar answered Sep 19 '22 19:09

Anisuzzaman Babla