tham7777

Untitled

Mar 9th, 2020
179
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.tham.biometric;
  2.  
  3. import android.app.KeyguardManager;
  4. import android.content.pm.PackageManager;
  5. import android.hardware.fingerprint.FingerprintManager;
  6. import android.Manifest;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.security.keystore.KeyGenParameterSpec;
  10. import android.security.keystore.KeyPermanentlyInvalidatedException;
  11. import android.security.keystore.KeyProperties;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.support.v4.app.ActivityCompat;
  14. import android.widget.TextView;
  15. import java.io.IOException;
  16. import java.security.InvalidAlgorithmParameterException;
  17. import java.security.InvalidKeyException;
  18. import java.security.KeyStore;
  19. import java.security.KeyStoreException;
  20. import java.security.NoSuchAlgorithmException;
  21. import java.security.NoSuchProviderException;
  22. import java.security.UnrecoverableKeyException;
  23. import java.security.cert.CertificateException;
  24. import javax.crypto.Cipher;
  25. import javax.crypto.KeyGenerator;
  26. import javax.crypto.NoSuchPaddingException;
  27. import javax.crypto.SecretKey;
  28.  
  29. import android.util.Log;
  30. import org.qtproject.qt5.android.bindings.QtActivity;
  31.  
  32. import net.vplay.helper.VPlayActivity;
  33.  
  34. import com.yourcompany.wizardEVAP.Fingertest.R;
  35.  
  36.  
  37. public class MainActivity extends VPlayActivity  {
  38.  
  39.     private static final String KEY_NAME = "yourKey";
  40.     private Cipher cipher;
  41.     private KeyStore keyStore;
  42.     private KeyGenerator keyGenerator;
  43.     private TextView textView;
  44.     private FingerprintManager.CryptoObject cryptoObject;
  45.     private FingerprintManager fingerprintManager;
  46.     private KeyguardManager keyguardManager;
  47.  
  48.     @Override
  49.     public void onCreate(Bundle savedInstanceState) {
  50.         Log.d("message", "on create MainActivity of fingerprint auth");
  51.         super.onCreate(savedInstanceState);
  52.         setContentView(R.layout.activity_main);
  53.  
  54.  
  55.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  56.  
  57.             Log.d("message", "Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN");
  58.             keyguardManager =
  59.                     (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
  60.                    
  61.             Log.d("message", "create keyguardManager");
  62.             fingerprintManager =
  63.                     (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
  64.  
  65.             Log.d("message", "create fingerprintManager");
  66.             textView = (TextView) findViewById(R.id.textview);
  67.             Log.d("message", "create textView");
  68.  
  69.             if (!fingerprintManager.isHardwareDetected()) {
  70.                 Log.d("message", "Your device doesn't support fingerprint authenticatio");
  71.                 textView.setText("Your device doesn't support fingerprint authentication");
  72.             }else{
  73.                 Log.d("message", "Your device support fingerprint authenticatio");
  74.                 textView.setText("Your device support fingerprint authentication");
  75.             }
  76.  
  77.             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
  78.                 Log.d("message", "Please enable the fingerprint permission");
  79.                 textView.setText("Please enable the fingerprint permission");
  80.             }
  81.  
  82.             if (!fingerprintManager.hasEnrolledFingerprints()) {
  83.                 Log.d("message", "No fingerprint configured. Please register at least one fingerprint in your device's Setting");
  84.                 textView.setText("No fingerprint configured. Please register at least one fingerprint in your device's Settings");
  85.  
  86.             }
  87.  
  88.             if (!keyguardManager.isKeyguardSecure()) {
  89.                 Log.d("message", "Please enable lockscreen security in your device's Setting");
  90.                 textView.setText("Please enable lockscreen security in your device's Settings");
  91.             } else {
  92.                 try {
  93.                     generateKey();
  94.                 } catch (FingerprintException e) {
  95.                     e.printStackTrace();
  96.                 }
  97.                 if (initCipher()){
  98.                     cryptoObject = new FingerprintManager.CryptoObject(cipher);
  99.                     Log.d("message", "create cryptoObject");
  100.                     FingerprintHandler helper = new FingerprintHandler(this);
  101.                     Log.d("message", "create helper");
  102.                     helper.startAuth(fingerprintManager, cryptoObject);
  103.                     Log.d("message", "start auth end");
  104.                 }
  105.             }
  106.  
  107.         }else{
  108.             Log.d("message", "Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN");
  109.         }
  110.     }
  111.  
  112.     private void generateKey() throws FingerprintException {
  113.         try {
  114.             Log.d("message", "generate key start");
  115.             keyStore = KeyStore.getInstance("AndroidKeyStore");
  116.             Log.d("message", "get keyStore");
  117.  
  118.             keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
  119.             Log.d("message", "create keyGenerator");
  120.  
  121.             keyStore.load(null);
  122.             Log.d("message", "init keystore");
  123.             keyGenerator.init(new
  124.                     KeyGenParameterSpec.Builder(KEY_NAME,
  125.                     KeyProperties.PURPOSE_ENCRYPT |
  126.                             KeyProperties.PURPOSE_DECRYPT)
  127.                     .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
  128.                     .setUserAuthenticationRequired(true)
  129.                     .setEncryptionPaddings(
  130.                             KeyProperties.ENCRYPTION_PADDING_PKCS7)
  131.                     .build());
  132.  
  133.             Log.d("message", "generate key start");
  134.             keyGenerator.generateKey();
  135.             Log.d("message", "generate key end");
  136.  
  137.         } catch (KeyStoreException
  138.                 | NoSuchAlgorithmException
  139.                 | NoSuchProviderException
  140.                 | InvalidAlgorithmParameterException
  141.                 | CertificateException
  142.                 | IOException exc) {
  143.             exc.printStackTrace();
  144.             throw new FingerprintException(exc);
  145.         }
  146.  
  147.  
  148.     }
  149.  
  150.  
  151.  
  152.     public boolean initCipher() {
  153.         try {
  154.             Log.d("message", "init cipher start");
  155.             cipher = Cipher.getInstance(
  156.                     KeyProperties.KEY_ALGORITHM_AES + "/"
  157.                             + KeyProperties.BLOCK_MODE_CBC + "/"
  158.                             + KeyProperties.ENCRYPTION_PADDING_PKCS7);
  159.             Log.d("message", "init cipher end");                           
  160.         } catch (NoSuchAlgorithmException |
  161.                 NoSuchPaddingException e) {
  162.             throw new RuntimeException("Failed to get Cipher", e);
  163.         }
  164.  
  165.         try {
  166.             keyStore.load(null);
  167.             Log.d("message", "create secret key");
  168.             SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
  169.                     null);
  170.             Log.d("message", "init cipher");                   
  171.             cipher.init(Cipher.ENCRYPT_MODE, key);
  172.             Log.d("message", "init cipher end");
  173.             return true;
  174.         } catch (KeyPermanentlyInvalidatedException e) {
  175.             return false;
  176.         } catch (KeyStoreException | CertificateException
  177.                 | UnrecoverableKeyException | IOException
  178.                 | NoSuchAlgorithmException | InvalidKeyException e) {
  179.             throw new RuntimeException("Failed to init Cipher", e);
  180.         }
  181.     }
  182.  
  183.  
  184.  
  185.     private class FingerprintException extends Exception {
  186.  
  187.         public FingerprintException(Exception e) {
  188.             super(e);
  189.         }
  190.     }
  191. }
RAW Paste Data