0

I have question i want to store my Keypairs using RSA encrption to encrypt my private credetials .i am using bellow code (Its is returning null when decrypting particular string by using there encrypted token(This happen when i reinstall my application)) .

 @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;
}
Umar Nawaz
  • 31
  • 7
  • 1
    Does this answer your question? [How Can I Use the Android KeyStore to securely store arbitrary strings?](https://stackoverflow.com/questions/27320610/how-can-i-use-the-android-keystore-to-securely-store-arbitrary-strings) – Swaminathan V Jun 21 '21 at 10:02
  • @RaguSwaminathan Thanks for replying my goal is that i want to encrypt my (email ) using keyStore encryption(RSA) and decrypt it when some other user see my profile – Umar Nawaz Jun 21 '21 at 10:21
  • can you show your code where you are encrypting/decrypting – Dharmender Manral Jun 21 '21 at 11:48
  • @DharmenderManral i am following this article"https://github.com/phanirajabhandari/android-keystore-example" – Umar Nawaz Jun 22 '21 at 07:10

0 Answers0