Getting InvalidKeyException while decrypting using private key

Viewed 499

I am trying to encrypt and decrypt data after generating public-private key pair in android key store. Somehow, it is failing during decryption.

I am using RSA/ECB/OAEPWithSHA-256AndMGF1Padding algorithm.

Below is my code

public void RSAEncryptDecryptTest() {
        String plain = "SampleData";
        try {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", ANDROID_KEY_STORE);
            AlgorithmParameterSpec spec;

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                // Below Android M, use the KeyPairGeneratorSpec.Builder.

                spec = new KeyPairGeneratorSpec.Builder(mApplicationContext)
                        // You'll use the alias later to retrieve the key.  It's a key for the key!
                        .setAlias(plain)
                        .build();
            } else {
            // On Android M or above, use the KeyGenparameterSpec.Builder and specify permitted
            // properties  and restrictions of the key.
            spec = new KeyGenParameterSpec.Builder(plain, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
                    | KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
                    /*.setKeySize(VisaSCPConstants.ALGORITHM_KEY_SIZE)*/
                    .setAlgorithmParameterSpec(new RSAKeyGenParameterSpec(VisaSCPConstants.ALGORITHM_KEY_SIZE, RSAKeyGenParameterSpec.F4))
                    .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                    .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
                    .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA1, KeyProperties.DIGEST_SHA512)
                    .setUserAuthenticationRequired(false)
                    .build();
            }

            kpg.initialize(spec);
            KeyPair kp = kpg.genKeyPair();
            PublicKey publicKey = kp.getPublic();
            PrivateKey privateKey = kp.getPrivate();

            Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = cipher.doFinal(plain.getBytes());
            System.out.println("Encrypted = " + Base64.encodeToString(encryptedBytes, Base64.NO_WRAP));

            cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte [] decryptedBytes = cipher.doFinal(encryptedBytes);
            if(plain.equals(new String(decryptedBytes))) {
                System.out.println("TRUE");
            } else {
                System.out.println("FALSE");
            }
        } catch (Exception e) {
            System.out.println("Exception " +e);
        }
    }

Below is the exception I am getting.

InvalidKeyException - No provider offers [RSA, ECB, OAEPWithSHA1AndMGF1Padding] for RSA key of class android.security.keystore.AndroidKeyStoreRSAPrivateKey and export format null

0 Answers
Related