写点什么

安卓指纹对称加密及登录功能的实现,阿里 P8 大牛从零开始教 Android 开源框架

用户头像
Android架构
关注
发布于: 刚刚

}


对称加密的主要实现步骤如下:


  1. 新建一个 KeyStore 密钥库,用于存放密钥;

  2. 获取 KeyGenerator 密钥生成工具,生成密钥;

  3. 通过密钥初始化 Cipher 对象,生成加密对象 CryptoObject;

  4. 调用 authenticate() 方法启动指纹传感器并开始监听。

1.新建一个 KeyStore 密钥库存放密钥:

/**


  • 创建 keystore

  • @throws Exception*/public CryptoObjectHelper() throws Exception {KeyStore _keystore = KeyStore.getInstance(KEYSTORE_NAME);_keystore.load(null);}

2.获取 KeyGenerator 密钥生成工具,生成密钥:

/**


  • 获取秘钥生成器,用于生成秘钥

  • @throws Exception*/public void CreateKey() throws Exception {KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM, KEYSTORE_NAME);KeyGenParameterSpec keyGenSpec =new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setBlockModes(BLOCK_MODE).setEncryptionPaddings(ENCRYPTION_PADDING).setUserAuthenticationRequired(true).build();keyGen.init(keyGenSpec);keyGen.generateKey();}

3.通过密钥初始化 Cipher 对象,生成加密对象 CryptoObject:

/**


  • @throws Exception

  • 密码生成,递归实现*/Cipher createCipher(boolean retry) throws Exception {Key key = GetKey();Cipher cipher = Cipher.getInstance(TRANSFORMATION);try {cipher.init(Cipher.ENCRYPT_MODE | Cipher.DECRYPT_MODE, key);} catch (KeyPermanentlyInvalidatedException e) {_keystore.deleteEntry(KEY_NAME);//删除获取的码,保留生成的密码 if (retry) {createCipher(false);} else {throw new Exception("Could not create the cipher", e);}}return cipher;}

4.调用 authenticate() 方法启动指纹传感器并开始监听:

CryptoObjectHelper cryptoObjectHelper = new CryptoObjectHelper();if (cancellationSignal == null) {cancellationSignal = new CancellationSignal();}fingerprintManager.authenticate(cryptoObjectHelper.buildCryptoObject(), 0,cancellationSignal, myAuthCallback, null);

最后我们在回调的类中监听指纹识别的结果:

public class MyAuthCallback extends FingerprintManagerCompat.AuthenticationCallback {


private Handler handler = null;


public MyAuthCallback(Handler handler) {super();this.handler = handler;}


/**


  • 验证错误信息*/@Overridepublic void onAuthenticationError(int errMsgId, CharSequence errString) {super.onAuthenticationError(errMsgId, errString);if (handler != null) {handler.obtainMessage(Constant.MSG_AUTH_ERROR, errMsgId, 0).sendToTarget();}}


/**


  • 身份验证帮助*/@Overridepublic void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {super.onAuthenticationHelp(helpMsgId, helpString);if (handler != null) {handler.obtainMessage(Constant.MSG_AUTH_HELP, helpMsgId, 0).sendToTarget();}}


/**


  • 验证成功*/@Overridepublic void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {super.onAuthenticationSucceeded(result);if (handler != null) {handler.obtainMessage(Constant.MSG_AUTH_SUCCESS).sendToTarget();}}


/**


  • 验证失败*/@Overridepublic void onAuthenticationFailed() {super.onAuthenticationFailed();if (handler != null) {handler.obtainMessage(Constant.MSG_AUTH_FAILED).sendToTarget();}}}




好了,上面一直讲的是对称加密以实现指纹识别;


接下来写了一个使用指纹进行登录的 demo 及封装(这里没有使用加密..):

我们先来看下我总结的指纹登录流程

指纹识别一定会有成功、失败等各种情况,所以先定义一个回调监听

/**


  • Description: 指纹识别回调

  • Created by jia on 2017/11/27.

  • 人之所以能,是相信能*/public interface FingerListener {


/**


  • 开始识别*/void onStartListening();


/**


  • 停止识别*/void onStopListening();


/**


  • 识别成功

  • @param result*/void onSuccess(FingerprintManager.AuthenticationResult result);


/**


  • 识别失败*/void onFail(boolean isNormal,String info);


/**


  • 多次识别失败 的 回调方法

  • @param errorCode

  • @param errString*/void onAuthenticationError(int errorCode, CharSequence errString);


/**


  • 识别提示*/void onAuthenticationHelp(int helpCode, CharSequence helpString);


}

1、先封装了指纹工具类

private FingerprintManager manager;private KeyguardManager mKeyManager;private CancellationSignal mCancellationSignal;//回调方法 private FingerprintManager.AuthenticationCallback mSelfCancelled;


private Context mContext;


private FingerListener listener;


指纹识别相关管理类当然是必须的了。

2、初始化它们

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {manager = (FingerprintManager) mContext.getSystemService(Context.FINGERPRI


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


NT_SERVICE);mKeyManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);mCancellationSignal = new CancellationSignal();initSelfCancelled();}

3、初始化系统的识别回调

private void initSelfCancelled() {mSelfCancelled = new FingerprintManager.AuthenticationCallback() {@Overridepublic void onAuthenticationError(int errorCode, CharSequence errString) {// 多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证 listener.onAuthenticationError(errorCode, errString);}


@Overridepublic void onAuthenticationHelp(int helpCode, CharSequence helpString) {listener.onAuthenticationHelp(helpCode, helpString);}


@Overridepublic void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {listener.onSuccess(result);}


@Overridepublic void onAuthenticationFailed() {listener.onFail(true, "识别失败");}};}

4、开始识别

/**


  • 开始监听识别*/public void startListening(FingerListener listener) {


this.listener = listener;

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
安卓指纹对称加密及登录功能的实现,阿里P8大牛从零开始教Android开源框架