我按照下面的示例 https://nelenkov.blogspot.in/2012/05/storing-application-secrets-in-androids.html 它工作正常,直到棉花糖中的棉花糖它给出了一些异常,我无法存储密钥库的键和值对。波纹管代码我得到异常对于这最后一步,似乎有两个选择:
keyStore.setKeyEntry(String alias, byte[] key, Certificate[] chain);
或者
keyStore.setKeyEntry(String alias, Key key, char[] password, Certificate[] chain);
我从两者中的第二个开始,但收到 java.security.KeyStoreException: entries cannot be protected with passwords .... 好吧,这很奇怪,为什么会有一种方法可以保证抛出异常?让我们试试 1 号门。
此时,当我调用 setKeyEntry 并将 keyPair.getPrivate().getEncoded() 作为第二个参数传递时,我收到 java.security.KeyStoreException: Operation not supported because key encoding is unknown from the system。
byte[] encodedKey = Base64.encode("keytostore".getBytes(),
Base64.DEFAULT);
SecretKey originalKey = new SecretKeySpec(encodedKey, 0,
encodedKey.length, "AES");
KeyStore instance = KeyStore.getInstance("AndroidKeyStore");
instance.load(null);
instance.setEntry(
"key1",
new KeyStore.SecretKeyEntry(originalKey),
new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT
| KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(
KeyProperties.ENCRYPTION_PADDING_NONE)
.build());