问题描述
首先,我不是android编程的新手,尽管我不是编程本身的新手.从本质上讲,我要做的就是将我的加密密钥保存到Android Keystore中. GOOGLE本身似乎缺少此类信息.由于该主题上的操作方法不多,因此我假设它不是相当标准的知识.所以有人可以给我一个示例代码来
First off I am new to android Programming, though I am not new to programming itself. What I am, essentially, trying to do is to save my encryption Keys into the Android Keystore. There seems to be a phenomenal lack of such information on GOOGLE, itself. Since there is not much how-to available on the topic I am assuming that it isn't fairly standard knowledge. So can someone please give me a sample code to
- 初始化KeyStore(将使用AES-256).
- 在KeyStore中保存多个密钥(请告诉我我可以在1个KeyStore中存储的最大密钥数,因为我计划不保存任何少于100个的密钥).
- 从KeyStore获取密钥.
- 编辑键
- 删除键
- 删除整个密钥库
因此,实质上,是用于密钥库的所有基本功能的代码.预先感谢您的协助.
So in essence a code for all basic functions of a keystore.Thank you in advance for your assistance.
推荐答案
如果您将minSdkVersion设置为23或更高版本,那么从本月开始,Android M可以轻松生成和管理对称密钥.
If you set your minSdkVersion to 23 or higher Android M makes it easy to generate and manage symmetric keys as of this month.
查看此处列出的第四个示例. https://developer.android.com/reference/android/security/keystore /KeyGenParameterSpec.html
Check out the 4th example listed here.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);
This example also was helpful.https://github.com/googlesamples/android-ConfirmCredential/blob/master/Application/src/main/java/com/example/android/confirmcredential/MainActivity.java
这篇关于Android KeyStore初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!