Android指紋識別
10461 ワード
開始前に、指紋識別ハードウェアを使用する基本的なステップを知る必要があります。 1.Android Manifest.xmlに次のような権限を申請します。2.FingerprintManagerの対象となる引用 3.運用中は、指紋識別装置などがあるかどうかを検査するための互換性である。上の手順を詳しく説明します。権限を説明するのは簡単です。Android Manifest.xmlに上記の権限を追加すればいいです。FingerprinentManagerを獲得する対象は、apper開発においてシステムサービスの対象となる一般的な方法である。///Using the Android Support Library v 4 finger prinentManager=FingerprinentManager Comp.from(this)。Using API level 23:fingerprint Manager=(Fingerprint Manager)gets System Service(Contect.FINGERPRINT_)SERVICE) 上記の2つの方法を示しましたが、最初はV 4サポートパッケージを通じて互換性のあるオブジェクト参照を得ることです。これはgoogleが推し進めるやり方です。また、appi 23 frame ebookのインターフェースを直接使用して対象参照を得ることもできます。
あとはそのまま使うか、暗号化を考えて使うことができます。ここでは主に説明をしていますので、操作しやすく、詳しくは説明しません。
使用時のデモを直接ご提供します。
私たちが使う時は主に指紋を開けて識別し、識別して成功し、失敗した場合はフィードバックする方法を使うからです。
多くのものは自分で理解し、多ければ多いほど良いです。
私は対称暗号を使っていますが、暗号化しなくても実行できます。
ここでは主にどのように使うかを説明しますが、指紋識別の流れを詳しく説明していません。
もっと知りたいのですが、クリックしてください。記事リンクを参照してください。
あとはそのまま使うか、暗号化を考えて使うことができます。ここでは主に説明をしていますので、操作しやすく、詳しくは説明しません。
使用時のデモを直接ご提供します。
私たちが使う時は主に指紋を開けて識別し、識別して成功し、失敗した場合はフィードバックする方法を使うからです。
多くのものは自分で理解し、多ければ多いほど良いです。
私は対称暗号を使っていますが、暗号化しなくても実行できます。
package com.zq.sensoradapter;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
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;
public class FingerActivity extends AppCompatActivity {
public static final String TAG = "Finger";
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fingerprint);
Button button = findViewById(R.id.sure_btn);
//
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createKey();
checkFinger();
}
});
}
/**
*
*
*/
@RequiresApi(api = Build.VERSION_CODES.M)
private void checkFinger(){
FingerprintManager fingerprintManager = getFingerprintManager(this);//
if (fingerprintManager == null){
Toast.makeText(this," ",Toast.LENGTH_LONG);
}else {
sureFinger(fingerprintManager);
}
}
/**
*
* @param context
* @return
*/
public static FingerprintManager getFingerprintManager(Context context) {
FingerprintManager fingerprintManager = null;
try {
fingerprintManager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
} catch (Throwable e) {
Log.e(TAG,"have not class FingerprintManager");
}
return fingerprintManager;
}
/**
*
*/
@RequiresApi(api = Build.VERSION_CODES.M)
private void sureFinger(FingerprintManager fingerprintManager){
// 。 CryptoObject Cipher :new FingerprintManager.CryptoObject(cipher)。
// CancellationSignal , 。 , new CancellationSignal() 。
// , 0。
// AuthenticationCallback , FingerprintManager 。 , 、 。 。
// Handler, , null。
fingerprintManager.authenticate(new FingerprintManager.CryptoObject(defaultCipher),
new CancellationSignal(), 0, new FingerprintManager.AuthenticationCallback() {
// , , 30s-60s。
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Log.e(TAG,errorCode +"onAuthenticationError :" + errString);
}
//
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
Log.e(TAG,helpCode + ":onAuthenticationHelp " + helpString);
}
//
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Log.e(TAG,"onAuthenticationSucceeded "+ result.getClass().getDeclaredFields());
for (Field params:result.getClass().getDeclaredFields()) {
Log.e(TAG,"onAuthenticationSucceeded params = "+ params.getName() );
}
try {
// mfrangerprint hide
// Fingerprint ( ,
// , , )
Field field = result.getClass().getDeclaredField("mFingerprint");
field.setAccessible(true);
Log.e(TAG,"getDeclaredField params = "+ field.getName());
Object fingerPrint = field.get(result);
Log.e(TAG,"field.get(result) params = "+ field.get(result));
/* Class> clzz = Class.forName("android.hardware.fingerprint.Fingerprint");
Method getName = clzz.getDeclaredMethod("getName");
Method getFingerId = clzz.getDeclaredMethod("getFingerId");
Method getGroupId = clzz.getDeclaredMethod("getGroupId");
Method getDeviceId = clzz.getDeclaredMethod("getDeviceId");
CharSequence name = (CharSequence) getName.invoke(fingerPrint);
int fingerId = (int) getFingerId.invoke(fingerPrint);
int groupId = (int) getGroupId.invoke(fingerPrint);
long deviceId = (long) getDeviceId.invoke(fingerPrint);
Log.d(TAG, "name: " + name);
Log.d(TAG, "fingerId: " + fingerId);
Log.d(TAG, "groupId: " + groupId);
Log.d(TAG, "deviceId: " + deviceId);*/
} catch (Exception e) {
e.printStackTrace();
}
}
// , , , onAuthenticationError
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Log.e(TAG,"onAuthenticationFailed ");
}
}, null);
}
//
KeyStore mKeyStore;// 、 (Key)
KeyGenerator mKeyGenerator;// , KeyGenerator
Cipher defaultCipher;//Cipher , 。
/**
* Cipher
*/
@RequiresApi(api = Build.VERSION_CODES.M)
private void createKey(){
// keyStore
/* KeyStore mKeyStore;
KeyGenerator mKeyGenerator;
Cipher defaultCipher;*/
try {
mKeyStore = KeyStore.getInstance("AndroidKeyStore");
} catch (KeyStoreException e) {
throw new RuntimeException("Failed to get an instance of KeyStore", e);
}
// , KeyGenerator
// KeyGenerator mKeyGenerator;
try {
mKeyGenerator = KeyGenerator
.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
}
String defaultKeyName = "crypto_object_fingerprint_key";
// KeyGenerator , Key
try {
mKeyStore.load(null);
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(defaultKeyName,
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();
} catch (CertificateException | NoSuchAlgorithmException | IOException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
// Cipher
// Cipher defaultCipher;
try {
defaultCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
+ KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException(" Cipher ", e);
}
// Cipher :
try {
mKeyStore.load(null);
SecretKey key = (SecretKey) mKeyStore.getKey(defaultKeyName, null);
defaultCipher.init(Cipher.ENCRYPT_MODE, key);
// return true;
} catch (IOException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyStoreException | InvalidKeyException e) {
throw new RuntimeException(" cipher ", e);
}
}
}
ここでは主にどのように使うかを説明しますが、指紋識別の流れを詳しく説明していません。
もっと知りたいのですが、クリックしてください。記事リンクを参照してください。