1

I use AES encryption in the mobile app, but on rare occasions I get these errors in the Application onCreate class. Method "aesEncrypt" is often called elsewhere in the program, but does not cause an error. It seems that in some cases the internal variables don't have time to initialize, which causes the error to occur. According to Crashlytics reports, the problem occurs on Android 10, 11 and 12.

Are there anyone who has encountered this problem and what can be done?

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
       at java.security.Provider$Service.supportsKeyFormat(Provider.java:1850)
       at java.security.Provider$Service.supportsParameter(Provider.java:1779)
       at javax.crypto.Cipher.tryCombinations(Cipher.java:2889)
       at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2796)
       at javax.crypto.Cipher.chooseProvider(Cipher.java:773)
       at javax.crypto.Cipher.init(Cipher.java:1288)
       at javax.crypto.Cipher.init(Cipher.java:1223)
       at my.app.name.EncKt.aesEncrypt(EncKt.java:123)
       my.app.name.App.onCreate (App.java:115)

Code:

fun aesEncrypt(cipher: String): String {
    try {
        val secretKeySpec = SecretKeySpec("abcdef1234567890".toByteArray(), "AES")
        val instance = Cipher.getInstance("AES/CBC/PKCS5Padding")
        instance.init(1, secretKeySpec, IvParameterSpec("0987654321fedcba".toByteArray()))
        return Base64.encodeToString(instance.doFinal(cipher.toByteArray()), Base64.NO_WRAP)
    } catch (e: Exception) {
        FirebaseCrashlytics.getInstance().recordException(e)
        return ""
    }
}

UPD: My question was closed and related to the general question about NullPointerException. That's just great, thanks, I know what NPE is! Dear moderators, before you close the question next time, pay attention to where the error occurred. These are internal components of the operating system. This is a specific question about a specific component and a possible error in the OS during application initialization.

Umy
  • 423
  • 4
  • 9
  • Are you sure you close the cipher objects? Remember you need to present minimal, complete, verifiable with an example so that one can work on your case. – kelalaka Jan 31 '22 at 19:54
  • @kelalaka, I don't close cipher object. And I shouldn't, according to Google example here: https://developer.android.com/guide/topics/security/cryptography#encrypt-message. In any case, the error occurs rarely and only from the onCreate method in Application when no other cipher objects exist yet. No error has ever occurred when using this method in other parts of the program. – Umy Jan 31 '22 at 21:08
  • We are also facing same issue after updating the java version from 8 to 11. – Manmohan Soni May 10 '22 at 10:56

0 Answers0