Реализована аутентификация по отпечатку пальца в приложении с подписью - PullRequest
0 голосов
/ 18 мая 2018

Я получаю проблему при запуске приложения ниже Android 6.0

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!fingerprintManager.isHardwareDetected()) {
            /**
             * An error message will be displayed if the device does not contain the fin.gerprint hardware.
             * However if you plan to implement a default authentication method,
             * you can redirect the user to a default authentication ctivity from here.
             * Example:
             * 
             */

        } else {


            // Checks whether fingerprint permission is set on manifest
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {

               // permission not granted
            } else {

                // Check whether at least one fingerprint is registered
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    // Checks whether lock screen security is enabled or not
                    if (!keyguardManager.isKeyguardSecure()) {
                        // finger print not support

                    } else {

                        if (!fingerprintManager.hasEnrolledFingerprints()) {
                        // finger print not enrolled

                        } else {

                            /*Amit Verma  EC signature*/
                            createKeyPair();
                            if (initSignature()) {
                                ConstantDeclaration.mCryptoObject = new FingerprintManager.CryptoObject(mSignature);
                            }
                        }
                    }
                }
            }
        }
    }
}

Вот код для генерации пары ключей

@TargetApi(Build.VERSION_CODES.M)
    private void createKeyPair() {
        // The enrolling flow for fingerprint. This is where you ask the user to set up fingerprint
        // for your flow. Use of keys is necessary if you need to know if the set of
        // enrolled fingerprints has changed.

        try {
            // Set the alias of the entry in Android KeyStore where the key will appear
            // and the constrains (purposes) in the constructor of the Builder

            KeyPairGenerator mKeyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
            mKeyPairGenerator.initialize(
                    new KeyGenParameterSpec.Builder(KEY_NAME,
                            KeyProperties.PURPOSE_SIGN)
                            .setDigests(KeyProperties.DIGEST_SHA1)
                            .setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1"))
                            // Require the user to authenticate with a fingerprint to authorize
                            // every use of the private key
//                            .setUserAuthenticationRequired(true)
                            .setUserAuthenticationRequired(false)
                            .build());
            mKeyPairGenerator.generateKeyPair();
        } catch (InvalidAlgorithmParameterException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }

    }

вот код для инициации подписи.

 @TargetApi(Build.VERSION_CODES.M)
    private boolean initSignature() {
        try {
            mKeyStore = KeyStore.getInstance("AndroidKeyStore");
            mKeyStore.load(null);
            PrivateKey key = (PrivateKey) mKeyStore.getKey(KEY_NAME, null);
//            String strKey = Base64.encodeToString(key.getEncoded(),Base64.DEFAULT);

//            System.out.println("PrivateKey::"+strKey);
            mSignature = Signature.getInstance("SHA1withECDSA");
            mSignature.initSign(key);

            PublicKey publicKey = mKeyStore.getCertificate(KEY_NAME).getPublicKey();
            String strPublicKey = Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
            Singleton.getInstance().public_key_fp = strPublicKey;

            return true;
        } catch (KeyPermanentlyInvalidatedException e) {
            return false;
        } catch (Exception e) {
            throw new RuntimeException("Failed to init Cipher", e);
        }/*catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
                | NoSuchAlgorithmException | InvalidKeyException e) {
            throw new RuntimeException("Failed to init Cipher", e);
        }*/
    }

Этот код работает нормально, но когда я запускаю свое приложение под Android 6.0, получаю исключение при запуске приложения.

Android version: 19
Device: samsung SM-J100ML
App version: 7
Line Number1: java.lang.Class.newInstanceImpl(Native Method)
Description : java.lang.VerifyError: launcherActivity
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1208)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
    at android.app.ActivityThread.access$800(ActivityThread.java:166)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5584)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
    at dalvik.system.NativeStart.main(Native Method)

Я думаю, что проблема с импортом подписиэто импорт java.security.Signature;

1 Ответ

0 голосов
/ 18 мая 2018

Я выяснил проблему, прочитав logcat, как сказано выше в комментарии:

Could not find class 'android.security.keystore.KeyGenParameterSpec$Builder'

и

Could not find class 'android.hardware.fingerprint.FingerprintManager'

Я погуглил над классами и нашел эту ссылку здесь

Итак, я убрал блок перехвата InvalidAlgorithmParameterException, **KeyPermanentlyInvalidatedException** и заменил на только родительский класс «Исключение» .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...