如何解决使用 RSA 加密将私钥存储在 AndroidkeyStore 中以解密加密字符串 Android
我有一个问题,我想使用 RSA encrption 来存储我的密钥对来加密我的私人凭证。我正在使用下面的代码(使用加密令牌解密特定字符串时它返回 null(这发生在我重新安装我的应用程序时)) .
@TargetApi(Build.VERSION_CODES.M) static SecurityKey generateSecretKey(KeyStore keyStore) {
try {
Log.i("keyStore","is: "+keyStore);
if (!keyStore.containsAlias(KEY_ALIAS)) {
KeyGenerator keyGenerator =
KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,ANDROID_KEY_STORE);
keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_ALIAS,KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setBlockModes(
KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setRandomizedEncryptionRequired(false)
.build());
return new SecurityKey(keyGenerator.generateKey());
}
} catch (KeyStoreException | NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
Timber.e(e);
}
try {
final KeyStore.SecretKeyEntry entry =
(KeyStore.SecretKeyEntry) keyStore.getEntry(KEY_ALIAS,null);
return new SecurityKey(entry.getSecretKey());
} catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableEntryException e) {
Timber.e(e);
}
return null;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。