· 6 years ago · Feb 01, 2019, 07:30 AM
1private SecretKey createKey() {
2 try {
3 KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
4 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
5 keyGenerator.init(new KeyGenParameterSpec.Builder("Key", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
6 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
7 .setUserAuthenticationRequired(true) //burayı kaldırırsan screen locka gerek kalmaz
8 .setUserAuthenticationValidityDurationSeconds(5)
9 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
10 .build());
11 return keyGenerator.generateKey();
12 }
13 } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
14 throw new RuntimeException("Failed to create a symmetric key", e);
15 }
16
17}