java - 使用 RSA 加密将私钥存储在 AndroidkeyStore 中以解密加密的字符串 Android
问题描述
我有一个问题,我想使用 RSA 加密存储我的密钥对来加密我的私人凭据。我正在使用波纹管代码(它在使用加密令牌解密特定字符串时返回 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;
}
解决方案
推荐阅读
- conv-neural-network - UnboundLocalError:分配前引用的局部变量“photoshop”
- python - 图像旋转错误的方式
- php - 如何在 WordPress 仪表板中添加多个链接?
- javascript - Apache Cordova 插件可以使用哪些 JavaScript 功能/版本?
- php - 如何使用 Propel 获取 MAX 和/或 MIN 聚合 mysql 函数?
- python - 使用 bqplot 将颜色图应用于线(或线段)
- sql - 关于 B 树索引中不同值的最大数量的小问题
- swift - Thread 1: Fatal error: Index out of range, return array.count + 1
- sql-server - 在 ColdFusion 2016 中将太多参数传递给存储过程时遇到问题
- cmake - 无法在 Debug / X64 的 Visual Studio 中调试 googletests