1

我有一个具有指纹身份验证的移动应用程序,它使用 xamarin 框架。到目前为止,除了三星 Note 8 之外,所有设备都运行良好。在尝试调试此设备时,它给了我一个异常 Java.Lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序。我不知道它是什么意思以及如何修复它,因为它适用于其他设备。它在行 keyGen.Init(keyGenSpec)上给出异常

    static readonly string KEY_NAME = "YYYYYYY";

    static readonly string KEYSTORE_NAME = "AndroidKeyStore";

    static readonly string KEY_ALGORITHM = KeyProperties.KeyAlgorithmAes;
    static readonly string BLOCK_MODE = KeyProperties.BlockModeCbc;
    static readonly string ENCRYPTION_PADDING = KeyProperties.EncryptionPaddingPkcs7;

    static readonly string TRANSFORMATION = KEY_ALGORITHM + "/" +
                                            BLOCK_MODE + "/" +
                                            ENCRYPTION_PADDING;

    readonly KeyStore _keystore;

    public CryptoObjectHelper()
    {
        _keystore = KeyStore.GetInstance(KEYSTORE_NAME);
        _keystore.Load(null);
    }

    public FingerprintManagerCompat.CryptoObject BuildCryptoObject()
    {
        Cipher cipher = CreateCipher();
        return new FingerprintManagerCompat.CryptoObject(cipher);
    }

    Cipher CreateCipher(bool retry = true)
    {
        IKey key = GetKey();
        Cipher cipher = Cipher.GetInstance(TRANSFORMATION);
        try
        {
            cipher.Init(CipherMode.EncryptMode, key);
        }
        catch (KeyPermanentlyInvalidatedException e)
        {
            _keystore.DeleteEntry(KEY_NAME);
            if (retry)
            {
                CreateCipher(false);
            }
            else
            {
                throw new Exception("Could not create the cipher for fingerprint authentication.", e);
            }
        }
        return cipher;
    }

    IKey GetKey()
    {
        if (!_keystore.IsKeyEntry(KEY_NAME))
        {
            CreateKey();
        }

        IKey secretKey = _keystore.GetKey(KEY_NAME, null);
        return secretKey;
    }

    void CreateKey()
    {

            KeyGenerator keyGen = KeyGenerator.GetInstance(KeyProperties.KeyAlgorithmAes, KEYSTORE_NAME);
            KeyGenParameterSpec keyGenSpec =
                new KeyGenParameterSpec.Builder(KEY_NAME, KeyStorePurpose.Encrypt | KeyStorePurpose.Decrypt)
                    .SetBlockModes(BLOCK_MODE)
                    .SetEncryptionPaddings(ENCRYPTION_PADDING)
                    .SetUserAuthenticationRequired(true)
                    .Build();
            keyGen.Init(keyGenSpec);
            keyGen.GenerateKey();


    }
4

2 回答 2

2

如果没有看到更多代码,很难知道发生了什么。看来您的代码是在后台线程上执行的。您可以尝试android.os.Looper.Prepare()CreateKey(). 希望它有效。

于 2018-03-07T14:12:55.380 回答
0

我不知道它是什么意思以及如何修复它,因为它适用于其他设备。它在行 keyGen.Init(keyGenSpec) 上给出异常

这是一个类似的问题。正如建议的那样,您可以根据以下内容捕获异常并初始化 UI 线程上的键failedOnNote8

failedOnNote8=false;
try{
    keyGen.Init(keyGenSpec);
}catch (Exception e) {
    if(e.getMessage() != null && e.getMessage().equals("Can't create handler inside thread that has not called Looper.prepare()"))  {
        failedOnNote8 = true;
    }
}
if(failedOnNote8)
{
   //try to init the key on UI Thread.
}
于 2018-03-08T09:00:28.553 回答