Android Decryption Error

后端 未结 3 782
甜味超标
甜味超标 2020-12-31 06:40

I am trying to encrypt and decrypt Strings in my Android application but I keep getting an InvalidKeyException error.

Here is my code:

//Generate Keys method

相关标签:
3条回答
  • 2020-12-31 07:34

    Removing the provider worked for me:

    Cipher.getInstance("RSA/ECB/PKCS1Padding")
    

    From Java docs: "This method traverses the list of registered security Providers, starting with the most preferred Provider. A new Cipher object encapsulating the CipherSpi implementation from the first Provider that supports the specified algorithm is returned."

    0 讨论(0)
  • 2020-12-31 07:40

    Try using a different provider, like this:

    Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidKeyStoreBCWorkaround");
    
    0 讨论(0)
  • 2020-12-31 07:41

    this worked for me:

    private Cipher getCipher() {
        try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // below android m
                return Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidOpenSSL"); // error in android 6: InvalidKeyException: Need RSA private or public key
            }
            else { // android m and above
                return Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidKeyStoreBCWorkaround"); // error in android 5: NoSuchProviderException: Provider not available: AndroidKeyStoreBCWorkaround
            }
        } catch(Exception exception) {
            throw new RuntimeException("getCipher: Failed to get an instance of Cipher", exception);
        }
    }
    
    0 讨论(0)
提交回复
热议问题