android监控指纹信息变化,android监听指纹变化(解决反射思路在android10不生效的问题)...
前天偶爾運(yùn)行代碼,一個(gè)段異常映入眼簾,我擦android10上反射機(jī)制監(jiān)聽不到指紋id等數(shù)據(jù)了,原因是android10徹底拋棄了之前指紋的api。所以反射不到了。
怎么解決這個(gè)問題?我們換個(gè)思路當(dāng)然反射依然可以,不過你需要在android9之前和之后的版本寫不同的反射代碼,首先之前的反射代碼都是我抄的別人的,android10現(xiàn)在還沒大神奉獻(xiàn),我也寫不出來!!!!(這是關(guān)鍵)
所以我想到了,
根據(jù)當(dāng)前指紋庫(kù)創(chuàng)建一個(gè)密鑰 判斷秘鑰狀態(tài)來判斷指紋庫(kù)是否發(fā)生了改變。是不是很神奇,不是原創(chuàng)的,我也是看見別人給了這樣一個(gè)思路。
我們?cè)O(shè)計(jì)一個(gè)吧!
1.設(shè)計(jì)一個(gè)初始化秘鑰的方法(這個(gè)方法要可以重新生成和重復(fù)利用之前秘鑰的功能,為什么?你問我為啥?因?yàn)橐O(jiān)聽改變就要能獲取上次的秘鑰所以要可以重復(fù)利用秘鑰,因?yàn)橐坏┲讣y變化了,用戶可能需要重置指紋重新設(shè)置指紋密碼,那么就要重新生成新的秘鑰。)
2,需要一個(gè)判斷指紋是否改變的方法,使用cipher去驗(yàn)證剛才的key,如果指紋改變了會(huì)拋異常,捕獲和這個(gè)異常就可以了。
不說廢話了上代碼:
package cn.lkk.lkk.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyPermanentlyInvalidatedException;
import android.security.keystore.KeyProperties;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
/**
* @author lkk
* @date 2019/08/23 10:01
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public class CipherHelper
{
private static CipherHelper instance;
private static final String DEFAULT_KEY_NAME = "defaultKey";
private static final String KEYSTORE_ALIAS = "keyStoreAlias";
private static final String HAS_FINGER_KEY = "hasFingerKey";
private KeyGenerator mKeyGenerator;
private KeyStore keyStore;
private CipherHelper()
{
try
{
keyStore = KeyStore.getInstance("AndroidKeyStore");
}
catch (KeyStoreException e)
{
e.printStackTrace();
}
try
{
mKeyGenerator = KeyGenerator
.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
}
catch (NoSuchAlgorithmException | NoSuchProviderException e)
{
throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
}
}
publicstatic CipherHelper getInstance()
{
if (instance == null)
{
synchronized (CipherHelper.class)
{
if (instance == null)
{
instance = new CipherHelper();
}
}
}
return instance;
}
/**
* @des 創(chuàng)建cipher
* @author lkk
* @date 2019/08/23 10:05
*/
publicCipher createCipher()
{
try
{
return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
+ KeyProperties.BLOCK_MODE_CBC + "/"
+ KeyProperties.ENCRYPTION_PADDING_PKCS7);
}
catch (NoSuchAlgorithmException | NoSuchPaddingException e)
{
e.printStackTrace();
}
return null;
}
/**
* @des 初始化Cipher ,并判斷指紋庫(kù)是否發(fā)生了變化
* @author lkk
* @date 2019/08/23 10:17
*/
publicboolean initCipher(Cipher cipher)
{
try
{
keyStore.load(null);
SecretKey key = (SecretKey) keyStore.getKey(KEYSTORE_ALIAS, null);
if (cipher == null)
{
cipher = createCipher();
}
cipher.init(Cipher.ENCRYPT_MODE, key);
return false;
}
catch (KeyPermanentlyInvalidatedException e)
{
return true;
}
catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
| NoSuchAlgorithmException | InvalidKeyException e)
{
return false;
//throw new RuntimeException("Failed to init Cipher", e);
}
}
/**
* @param context
* @param createNewKey 是否創(chuàng)建新的密鑰
* @des 根據(jù)當(dāng)前指紋庫(kù)創(chuàng)建一個(gè)密鑰
* @author lkk
* @date 2019/08/23 10:30
*/
publicvoid createKey(Context context, boolean createNewKey)
{
if (context == null)
{
throw new RuntimeException("context can not be null");
}
SharedPreferences sharedPreferences = context.getSharedPreferences(DEFAULT_KEY_NAME, Context.MODE_PRIVATE);
try
{
//首先通過標(biāo)志位判斷用戶之前是否創(chuàng)建了密鑰,如果已經(jīng)創(chuàng)建過了并且不需要重新創(chuàng)建就跳過
if (TextUtils.isEmpty(sharedPreferences.getString(HAS_FINGER_KEY, "")) || createNewKey)
{
//創(chuàng)建新密鑰
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(KEYSTORE_ALIAS,
KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
builder.setInvalidatedByBiometricEnrollment(true);
}
mKeyGenerator.init(builder.build());
mKeyGenerator.generateKey();
sharedPreferences.edit().putString(HAS_FINGER_KEY, "KEY").apply();
}
}
catch (InvalidAlgorithmParameterException e)
{
//throw new RuntimeException(e);
}
}
}
怎么使用呢?
開始監(jiān)聽
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCipher = CipherHelper.getInstance().createCipher();
CipherHelper.getInstance().createKey(getActivity(), false);
}
監(jiān)聽判斷
if (CipherHelper.getInstance().initCipher(mCipher)){
//關(guān)閉指紋登錄
//彈窗告知用戶
}
重置監(jiān)聽Key
CipherHelper.getInstance().createKey(getActivity(), true);
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的android监控指纹信息变化,android监听指纹变化(解决反射思路在android10不生效的问题)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 输卵管粘连手术一共需要多少钱?
- 下一篇: android git上传出现错误,热更