Android keystore 初始化

标签 android encryption cryptography aes keystore

首先,我是 android 编程的新手,尽管我对编程本身并不陌生。本质上,我要做的是将我的加密 key 保存到 Android keystore 中。 GOOGLE 本身似乎非常缺乏此类信息。由于没有太多关于该主题的操作方法,我假设它不是相当标准的知识。那么有人可以给我一个示例代码吗

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

所以本质上是 keystore 所有基本功能的代码。 预先感谢您的协助。

最佳答案

如果您将 minSdkVersion 设置为 23 或更高版本,从本月开始,Android M 可以轻松生成和管理对称 key 。

查看此处列出的第 4 个示例。 https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html

 KeyGenerator keyGenerator = KeyGenerator.getInstance(
         KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
 keyGenerator.init(
         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);

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

关于Android keystore 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28804307/

相关文章:

android - 如果 webview 中没有可用的 favicon,则显示 favicon-default.png

android - DISTINCT 与 RANDOM() 排序

Android在点击时更改谷歌地图标记图标

node.js - 如果在加密之前已知明文的开头是否存在漏洞?

encryption - 为什么 AES 的不同实现会产生不同的输出?

javax.crypto.BadPaddingException : Message is larger than modulus

Golang AES-CBC 256 使用 CryptoJS 解密

java - 如何写入分区/数据文件?

encryption - 如何混淆加密功能的 key ?

c# - ProtectedData.Protect 间歇性故障