2016-08-24 40 views
0

我是android生物指標集成的新手。使用指紋傳感器作爲生物指標

是否可以將android設備屏幕或指紋傳感器用作生物識別設備,以便用戶無需使用任何電子郵件或密碼即可登錄到應用程序。

thisthisthis作爲參考。

正在使用外部指紋掃描儀或生物識別唯一的選擇還是有其他解決方案?

這是我用作參考的代碼。請看看它,讓我知道如何獲得用戶指紋。

MainActivity extends AppCompatActivity { 

private FingerprintManager fingerprintManager; 
private KeyguardManager keyguardManager; 
private KeyStore keyStore; 
private KeyGenerator keyGenerator; 
private static final String KEY_NAME = "example_key"; 
private Cipher cipher; 
private FingerprintManager.CryptoObject cryptoObject; 

@TargetApi(Build.VERSION_CODES.M) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    keyguardManager = 
      (KeyguardManager) getSystemService(KEYGUARD_SERVICE); 
    fingerprintManager = 
      (FingerprintManager) getSystemService(FINGERPRINT_SERVICE); 


    if (!keyguardManager.isKeyguardSecure()) { 

     Toast.makeText(this, 
       "Lock screen security not enabled in Settings", 
       Toast.LENGTH_LONG).show(); 
     return; 
    } 

    if (ActivityCompat.checkSelfPermission(this, 
      Manifest.permission.USE_FINGERPRINT) != 
      PackageManager.PERMISSION_GRANTED) { 
     Toast.makeText(this, 
       "Fingerprint authentication permission not enabled", 
       Toast.LENGTH_LONG).show(); 

     return; 
    } 

    if (!fingerprintManager.hasEnrolledFingerprints()) { 

     // This happens when no fingerprints are registered. 
     Toast.makeText(this, 
       "Register at least one fingerprint in Settings", 
       Toast.LENGTH_LONG).show(); 
     return; 
    } 

    if (!fingerprintManager.hasEnrolledFingerprints()) { 

     // This happens when no fingerprints are registered. 
     Toast.makeText(this, 
       "Register at least one fingerprint in Settings", 
       Toast.LENGTH_LONG).show(); 
     return; 
    } 

    generateKey(); 

    if (cipherInit()) { 
     cryptoObject = 
       new FingerprintManager.CryptoObject(cipher); 
     FingerprintHandler helper = new FingerprintHandler(this); 
     helper.startAuth(fingerprintManager, cryptoObject); 
    } 

} 

@TargetApi(Build.VERSION_CODES.M) 
protected void generateKey() { 
    try { 
     keyStore = KeyStore.getInstance("AndroidKeyStore"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 
     keyGenerator = KeyGenerator.getInstance(
       KeyProperties.KEY_ALGORITHM_AES, 
       "AndroidKeyStore"); 
    } catch (NoSuchAlgorithmException | 
      NoSuchProviderException e) { 
     throw new RuntimeException(
       "Failed to get KeyGenerator instance", e); 
    } 

    try { 
     keyStore.load(null); 
     keyGenerator.init(new 
       KeyGenParameterSpec.Builder(KEY_NAME, 
       KeyProperties.PURPOSE_ENCRYPT | 
         KeyProperties.PURPOSE_DECRYPT) 
       .setBlockModes(KeyProperties.BLOCK_MODE_CBC) 
       .setUserAuthenticationRequired(true) 
       .setEncryptionPaddings(
         KeyProperties.ENCRYPTION_PADDING_PKCS7) 
       .build()); 
     keyGenerator.generateKey(); 
    } catch (NoSuchAlgorithmException | 
      InvalidAlgorithmParameterException 
      | CertificateException | IOException e) { 
     throw new RuntimeException(e); 
    } 
} 

@TargetApi(Build.VERSION_CODES.M) 
public boolean cipherInit() { 
    try { 
     cipher = Cipher.getInstance(
       KeyProperties.KEY_ALGORITHM_AES + "/" 
         + KeyProperties.BLOCK_MODE_CBC + "/" 
         + KeyProperties.ENCRYPTION_PADDING_PKCS7); 
    } catch (NoSuchAlgorithmException | 
      NoSuchPaddingException e) { 
     throw new RuntimeException("Failed to get Cipher", e); 
    } 

    try { 
     keyStore.load(null); 
     SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, 
       null); 
     cipher.init(Cipher.ENCRYPT_MODE, key); 
     return true; 
    } catch (KeyPermanentlyInvalidatedException e) { 
     return false; 
    } catch (KeyStoreException | CertificateException 
      | UnrecoverableKeyException | IOException 
      | NoSuchAlgorithmException | InvalidKeyException e) { 
     throw new RuntimeException("Failed to init Cipher", e); 
    } 
} 
} 
+0

嗨@mohammed。歡迎來到stackoverflow。您應該提供部分代碼並提出具體問題。你不能期望社區:在我的工作中問我這個。我該怎麼做? :)。隊友的歡呼聲。 –

+0

eliasMP @我不知道如何stackoverflow工作。 –

+0

您好@mohammed您可以查看下面的鏈接:https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ http://catb.org/esr/faqs/smart- questions.html和StackOverflow論壇:http://stackoverflow.com/help/question-bans –

回答

0

我找到了我自己的解決方案。事實證明,生物度量設備在指紋被它們讀取時提供加密數據。我認爲由於安全原因,手機中存在的正常指紋傳感器不提供這種加密數據。開發人員用指紋傳感器可以做的最接近的事情是找出指紋是否與Android設備中已經註冊的指紋匹配。正因爲如此,我不得不使用第三方sdk。