I am learning Android Development and tried to work on Fingerprint Authentication So I watch a video on Youtube about it and do the same thing shown in that video, First, there is try-catch block through which my app doesn't crash but I'm unable to find the error so I removed the Try-Catch block. While I am doing this I got this error. Here is my Main Activity Code.
package com.jayu.learningnewthings
import android.Manifest
import android.app.KeyguardManager
import android.content.pm.PackageManager
import android.hardware.fingerprint.FingerprintManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import java.lang.Exception
import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
@RequiresApi(Build.VERSION_CODES.M)
class FingerPrintActivity : AppCompatActivity() {
lateinit var fm : FingerprintManager
lateinit var km : KeyguardManager
lateinit var keyStore : KeyStore
lateinit var keyGenerator: KeyGenerator
var keyName = "FingerPrint"
lateinit var cipher: Cipher
lateinit var cryptoObject: FingerprintManager.CryptoObject
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_finger_print)
km = getSystemService(KEYGUARD_SERVICE) as KeyguardManager
fm = getSystemService(FINGERPRINT_SERVICE) as FingerprintManager
if (!km.isKeyguardSecure){
Toast.makeText(this, "Lock Screen Not Enabled", Toast.LENGTH_SHORT).show()
return
}
if (!fm.hasEnrolledFingerprints()){
Toast.makeText(this, "Please Enroll one fingerprint", Toast.LENGTH_SHORT).show()
return
}
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.USE_FINGERPRINT), 111)
}
else{
validateFingerPrint()
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 111 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
validateFingerPrint()
}
}
private fun validateFingerPrint() {
//Generating Key
//Catch 1
try{
keyStore = KeyStore.getInstance("AndroidKeyStore")
keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,"AndroidKeyStore")
keyStore.load(null)
keyGenerator.init(KeyGenParameterSpec.Builder(keyName
,KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(false)
.build())
keyGenerator.generateKey()
}catch (e : Exception){
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show()
}
//Initialize of Cryptography
if (initCipher()) {
cipher.let {
cryptoObject = FingerprintManager.CryptoObject(it)
}
val helper = FingerPrintHelper(this)
if (fm != null && cryptoObject != null){
helper.startAuth(fm,cryptoObject)
}
}
}
private fun initCipher(): Boolean {
val cipherAlgorithm = "${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}"
cipher = Cipher.getInstance(cipherAlgorithm)
keyStore.load(null)
val key = keyStore.getKey(keyName,null) as SecretKey
cipher.init(Cipher.ENCRYPT_MODE,key)
return true
}
}
The Full-stack of error is
App restart successful without requiring a re-install.
$ adb shell am start -n "com.jayu.learningnewthings/com.jayu.learningnewthings.FingerPrintActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 19230 on device 'Pixel_2_API_29 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/arningnewthing: Not late-enabling -Xcheck:jni (already on)
E/arningnewthing: Unknown bits set in runtime_flags: 0x8000
W/arningnewthing: Unexpected CPU variant for X86 using defaults: x86
D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
W/RenderThread: type=1400 audit(0.0:231): avc: denied { write } for name="property_service" dev="tmpfs" ino=7194 scontext=u:r:untrusted_app:s0:c142,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.jayu.learningnewthings
W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
W/arningnewthing: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/arningnewthing: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jayu.learningnewthings, PID: 19230
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jayu.learningnewthings/com.jayu.learningnewthings.FingerPrintActivity}: java.security.InvalidKeyException: Keystore operation failed
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.security.InvalidKeyException: Keystore operation failed
at android.security.KeyStore.getInvalidKeyException(KeyStore.java:1362)
at android.security.KeyStore.getInvalidKeyException(KeyStore.java:1402)
at android.security.keystore.KeyStoreCryptoOperationUtils.getInvalidKeyExceptionForInit(KeyStoreCryptoOperationUtils.java:54)
at android.security.keystore.KeyStoreCryptoOperationUtils.getExceptionForCipherInit(KeyStoreCryptoOperationUtils.java:89)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.ensureKeystoreOperationInitialized(AndroidKeyStoreCipherSpiBase.java:265)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:109)
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2984)
at javax.crypto.Cipher.tryCombinations(Cipher.java:2891)
at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2796)
at javax.crypto.Cipher.chooseProvider(Cipher.java:773)
at javax.crypto.Cipher.init(Cipher.java:1143)
at javax.crypto.Cipher.init(Cipher.java:1084)
at com.jayu.learningnewthings.FingerPrintActivity.initCipher(FingerPrintActivity.kt:111)
at com.jayu.learningnewthings.FingerPrintActivity.validateFingerPrint(FingerPrintActivity.kt:82)
at com.jayu.learningnewthings.FingerPrintActivity.onCreate(FingerPrintActivity.kt:51)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)?
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)?
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)?
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)?
at android.os.Handler.dispatchMessage(Handler.java:107)?
at android.os.Looper.loop(Looper.java:214)?
at android.app.ActivityThread.main(ActivityThread.java:7356)?
at java.lang.reflect.Method.invoke(Native Method)?
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)?
Caused by: android.security.KeyStoreException: Incompatible padding mode
at android.security.KeyStore.getKeyStoreException(KeyStore.java:1292)
at android.security.KeyStore.getInvalidKeyException(KeyStore.java:1402)?
at android.security.keystore.KeyStoreCryptoOperationUtils.getInvalidKeyExceptionForInit(KeyStoreCryptoOperationUtils.java:54)?
at android.security.keystore.KeyStoreCryptoOperationUtils.getExceptionForCipherInit(KeyStoreCryptoOperationUtils.java:89)?
at android.security.keystore.AndroidKeyStoreCipherSpiBase.ensureKeystoreOperationInitialized(AndroidKeyStoreCipherSpiBase.java:265)?
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:109)?
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2984)?
at javax.crypto.Cipher.tryCombinations(Cipher.java:2891)?
at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2796)?
at javax.crypto.Cipher.chooseProvider(Cipher.java:773)?
at javax.crypto.Cipher.init(Cipher.java:1143)?
at javax.crypto.Cipher.init(Cipher.java:1084)?
at com.jayu.learningnewthings.FingerPrintActivity.initCipher(FingerPrintActivity.kt:111)?
at com.jayu.learningnewthings.FingerPrintActivity.validateFingerPrint(FingerPrintActivity.kt:82)?
at com.jayu.learningnewthings.FingerPrintActivity.onCreate(FingerPrintActivity.kt:51)?
at android.app.Activity.performCreate(Activity.java:7802)?
at android.app.Activity.perfor
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…