Android 9 - KeyStore exception android.os.ServiceSpecificException

后端 未结 3 607
时光说笑
时光说笑 2020-12-04 18:11

If I run this code on Android 9, I receive the follow exception:

private static KeyStore.PrivateKeyEntry getPrivateKeyEntry(String alias) {
        try {
            


        
3条回答
  •  甜味超标
    2020-12-04 18:34

    I had the same problem retrieving an asymmetricKey from AndroidKeyStore

    My solution based on Dr Glass answer to get keys is the following: (aliasKey is your alias string)

    PublicKey:

    val keyStore = KeyStore.getInstance("AndroidKeyStore")
    keyStore.load(null)
    
    val asymmetricPublicKey = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        keyStore.getCertificate(aliasKey).publicKey
    } else {
        val asymmetricKey = keyStore.getEntry(aliasKey, null) as KeyStore.PrivateKeyEntry
        asymmetricKey.certificate.publicKey
    }
    

    PrivateKey:

    val keyStore = KeyStore.getInstance("AndroidKeyStore")
    keyStore.load(null)
    
    val asymmetricPrivateKey = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        keyStore.getKey(aliasKey, null) as PrivateKey
    } else {
        val asymmetricKey = keyStore.getEntry(aliasKey, null) as KeyStore.PrivateKeyEntry
        asymmetricKey.privateKey
    }
    

    and with this code I have no warning in the emulator and/or device with Android P

提交回复
热议问题