使用私钥解密时获取InvalidKeyException

时间:2019-10-09 05:25:54

标签: android encryption cryptography rsa android-keystore

我试图在android密钥存储区中生成公私钥对后对数据进行加密和解密。不知何故,它在解密过程中失败了。

我正在使用RSA / ECB / OAEPWithSHA-256AndMGF1Padding算法。

下面是我的代码

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);
        }
    }

以下是我得到的例外情况。

InvalidKeyException-没有提供者为类android.security.keystore.AndroidKeyStoreRSAPrivateKey的RSA密钥提供[RSA,ECB,OAEPWithSHA1AndMGF1Padding]和导出格式为空

0 个答案:

没有答案