{"record":{"id":"1851a0d0a8a75a96","repo":"MuntashirAkon/AppManager","slug":"no-keypair-with-alias-rsa-key-alias-rsacrypto","errorCode":null,"errorMessage":"No KeyPair with alias RSA_KEY_ALIAS","messagePattern":"No KeyPair with alias RSA_KEY_ALIAS","errorType":"exception","errorClass":"CryptoException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/crypto/RSACrypto.java","lineNumber":84,"sourceCode":"            Cipher cipher = Cipher.getInstance(RSA_CIPHER_TYPE);\n            cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivateKey());\n            return new SecretKeySpec(cipher.doFinal(encryptedAesKey), \"AES\");\n        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException\n                | IllegalBlockSizeException e) {\n            throw new CryptoException(e);\n        }\n    }\n\n    @NonNull\n    static byte[] encryptAesKey(@NonNull SecretKey key) throws CryptoException {\n        // We only have 32/64 bytes AES key with either 256 or 512 bytes minus 42 bytes of data,\n        // so it should work without issues\n        KeyPair keyPair;\n        try {\n            KeyStoreManager keyStoreManager = KeyStoreManager.getInstance();\n            keyPair = keyStoreManager.getKeyPair(RSA_KEY_ALIAS);\n            if (keyPair == null) {\n                throw new CryptoException(\"No KeyPair with alias \" + RSA_KEY_ALIAS);\n            }\n        } catch (Exception e) {\n            throw new CryptoException(e);\n        }\n        try {\n            Cipher cipher = Cipher.getInstance(RSA_CIPHER_TYPE);\n            cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublicKey());\n            return cipher.doFinal(key.getEncoded());\n        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException\n                | IllegalBlockSizeException e) {\n            throw new CryptoException(e);\n        }\n    }\n}\n","sourceCodeStart":66,"sourceCodeEnd":99,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/crypto/RSACrypto.java#L66-L99","documentation":"RSACrypto.encryptAesKey needs the RSA KeyPair under RSA_KEY_ALIAS to wrap the session AES key. When KeyStoreManager returns null (no such entry) or throws, CryptoException is thrown, aborting encryption.","triggerScenarios":"Calling encryptAesKey (or encrypt via AESCrypto in RSA mode) before generating the RSA key pair, or when keystore access fails (provider error, keystore corrupted).","commonSituations":"First run where key generation was skipped; crypto initialized with RSA mode but keys never provisioned; AndroidKeyStore failures after system update or locked/limited device profile.","solutions":["Generate the RSA key pair for RSA_KEY_ALIAS via KeyStoreManager before encrypting","Verify AndroidKeyStore availability and that no ProviderException is wrapped in the CryptoException","Recreate the crypto instance after key generation so it picks up the new KeyPair"],"exampleFix":"// before\nbyte[] enc = RSACrypto.encryptAesKey(aesKey); // no keypair yet\n// after\nKeyStoreManager ksm = KeyStoreManager.getInstance();\nif (ksm.getKeyPair(RSA_KEY_ALIAS) == null) ksm.generateKeyPair(RSA_KEY_ALIAS);\nbyte[] enc = RSACrypto.encryptAesKey(aesKey);","handlingStrategy":"validation","validationCode":"KeyPair kp = KeyStoreManager.getInstance().getKeyPair(RSA_KEY_ALIAS);\nif (kp == null) {\n    KeyStoreManager.getInstance().generateKeyPair(RSA_KEY_ALIAS);\n}","typeGuard":null,"tryCatchPattern":"try {\n    byte[] enc = RSACrypto.encryptAesKey(aesKey);\n} catch (CryptoException e) {\n    // regenerate key pair and retry once; surface persistent keystore errors\n}","preventionTips":["Provision RSA keys at first-run/setup","Verify AndroidKeyStore health before crypto use (catch ProviderException)","Re-check key existence after any OS upgrade before encrypting"],"tags":["crypto","android","keystore","rsa","missing-key"],"backgroundTag":"resource-not-found","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}