Android KeyStore初始化

Pie*_*aux 1 encryption android cryptography aes keystore

首先,我是Android编程的新手,虽然我不是编程本身的新手.实际上,我的目的是将加密密钥保存到Android密钥库中.GOOGLE本身似乎非常缺乏此类信息.由于关于该主题的方法不多,我假设它不是相当标准的知识.那么有人可以给我一个示例代码

  1. 初始化KeyStore(将使用AES-256).
  2. 在KeyStore中保存多个密钥(请告诉我可以在1个KeyStore中存储的最大密钥数,因为我计划保存100个以下的密钥).
  3. 从KeyStore获取密钥.
  4. 编辑密钥
  5. 删除密钥
  6. 删除整个KeyStore

所以本质上是密钥库的所有基本功能的代码.提前感谢您的协助.

nbu*_*n42 6

如果将minSdkVersion设置为23或更高,Android M可以在本月轻松生成和管理对称密钥.

看看这里列出的第四个例子. https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html

 KeyGenerator keyGenerator = KeyGenerator.getInstance(
         KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
 keyGenerator.initialize(
         new KeyGenParameterSpec.Builder("key2",
                 KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                 .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                 .build());
 SecretKey key = keyGenerator.generateKey();

 Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
 cipher.init(Cipher.ENCRYPT_MODE, key);
 ...

 // The key can also be obtained from the Android Keystore any time as follows:
 KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
 keyStore.load(null);
 key = (SecretKey) keyStore.getKey("key2", null);
Run Code Online (Sandbox Code Playgroud)

这个例子也很有帮助. https://github.com/googlesamples/android-ConfirmCredential/blob/master/Application/src/main/java/com/example/android/confirmcredential/MainActivity.java

  • 如果您想使用相同的解决方案同时支持旧设备(即 API 18)和新设备(API 23+)怎么办? (2认同)