首页 > 解决方案 > 使用 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;
}

标签: javaandroidencryptionandroid-keystore

解决方案


推荐阅读