· 6 years ago · Dec 01, 2019, 04:12 PM
1package com.example.fingerfinal;
2
3import androidx.appcompat.app.AppCompatActivity;
4import androidx.core.content.ContextCompat;
5
6import android.Manifest;
7import android.annotation.TargetApi;
8import android.app.KeyguardManager;
9import android.content.Context;
10import android.content.pm.PackageManager;
11import android.hardware.fingerprint.FingerprintManager;
12import android.os.Build;
13import android.os.Bundle;
14import android.security.keystore.KeyGenParameterSpec;
15import android.security.keystore.KeyPermanentlyInvalidatedException;
16import android.security.keystore.KeyProperties;
17import android.view.View;
18import android.widget.Button;
19import android.widget.ImageView;
20import android.widget.TextView;
21import android.widget.Toast;
22
23import java.io.IOException;
24import java.io.UnsupportedEncodingException;
25import java.security.InvalidAlgorithmParameterException;
26import java.security.InvalidKeyException;
27import java.security.KeyStore;
28import java.security.KeyStoreException;
29import java.security.NoSuchAlgorithmException;
30import java.security.NoSuchProviderException;
31import java.security.UnrecoverableKeyException;
32import java.security.cert.CertificateException;
33
34import javax.crypto.BadPaddingException;
35import javax.crypto.Cipher;
36import javax.crypto.IllegalBlockSizeException;
37import javax.crypto.KeyGenerator;
38import javax.crypto.NoSuchPaddingException;
39import javax.crypto.SecretKey;
40
41public class MainActivity extends AppCompatActivity {
42
43
44
45
46 private TextView mHeadingLabel;
47 private ImageView mFingerprintImage;
48 private TextView mParaLabel;
49
50 private FingerprintManager fingerprintManager;
51 private KeyguardManager keyguardManager;
52
53 private KeyStore keyStore;
54 private Cipher cipher;
55 private String KEY_NAME = "AndroidKey";
56 private Context context;
57 Button button;
58 String salt = "salt";
59 String mess = "witam";
60 @Override
61 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63 setContentView(R.layout.activity_main);
64
65 mHeadingLabel = (TextView) findViewById(R.id.headingLabel);
66 mFingerprintImage = (ImageView) findViewById(R.id.fingerprintImage);
67 mParaLabel = (TextView) findViewById(R.id.paraLabel);
68 button = (Button) findViewById(R.id.button);
69
70 button.setOnClickListener(new View.OnClickListener() {
71 @Override
72 public void onClick(View v) {
73 try {
74 keyStore = KeyStore.getInstance("AndroidKeyStore");
75 } catch (KeyStoreException e) {
76 e.printStackTrace();
77 }
78
79 try {
80 keyStore.load(null);
81 } catch (CertificateException e) {
82 e.printStackTrace();
83 } catch (IOException e) {
84 e.printStackTrace();
85 } catch (NoSuchAlgorithmException e) {
86 e.printStackTrace();
87 }
88
89 Cipher cipher3 = null;
90 try {
91 cipher3 = getCipher();
92 } catch (NoSuchPaddingException e) {
93 e.printStackTrace();
94 } catch (NoSuchAlgorithmException e) {
95 e.printStackTrace();
96 }
97
98 SecretKey key = null;
99 try {
100 key = (SecretKey) keyStore.getKey(KEY_NAME,
101 null);
102 } catch (KeyStoreException e) {
103 e.printStackTrace();
104 } catch (NoSuchAlgorithmException e) {
105 e.printStackTrace();
106 } catch (UnrecoverableKeyException e) {
107 e.printStackTrace();
108 }
109
110 try {
111 cipher3.init(Cipher.ENCRYPT_MODE,key);
112 } catch (InvalidKeyException e) {
113 e.printStackTrace();
114 }
115
116
117 }
118 });
119
120 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
121
122 fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
123 keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
124
125 if(!fingerprintManager.isHardwareDetected()){
126
127 mParaLabel.setText("Nie odnaleziono skanera w urządzeniu");
128
129 } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED){
130
131 mParaLabel.setText("Brak uprawnień do skanera");
132
133 } else if (!keyguardManager.isKeyguardSecure()){
134
135 mParaLabel.setText("Dodaj blokade w telefonie");
136
137 } else if (!fingerprintManager.hasEnrolledFingerprints()){
138
139 mParaLabel.setText("Dodaj przynajmniej jeden odcisk palca");
140
141 } else {
142
143 mParaLabel.setText("Umiesc palec w celu weryfikacaji");
144
145 generateKey();
146
147 if (cipherInit()){
148
149 FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
150 FingerprintHandler fingerprintHandler = new FingerprintHandler(this);
151 fingerprintHandler.startAuth(fingerprintManager, cryptoObject);
152 }
153 }
154
155 }
156
157 }
158
159 @TargetApi(Build.VERSION_CODES.M)
160 private void generateKey() {
161 try {
162
163 keyStore = KeyStore.getInstance("AndroidKeyStore");
164 KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
165
166 keyStore.load(null);
167 keyGenerator.init(new
168 KeyGenParameterSpec.Builder(KEY_NAME,
169 KeyProperties.PURPOSE_ENCRYPT |
170 KeyProperties.PURPOSE_DECRYPT)
171 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
172 .setUserAuthenticationRequired(true)
173 .setEncryptionPaddings(
174 KeyProperties.ENCRYPTION_PADDING_PKCS7)
175 .build());
176 keyGenerator.generateKey();
177
178 } catch (KeyStoreException | IOException | CertificateException
179 | NoSuchAlgorithmException | InvalidAlgorithmParameterException
180 | NoSuchProviderException e) {
181
182 e.printStackTrace();
183
184 }
185
186 }
187
188 @TargetApi(Build.VERSION_CODES.M)
189 public boolean cipherInit() {
190 try {
191 cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
192 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
193 throw new RuntimeException("Błąd Ciphera", e);
194 }
195
196 try {
197
198 keyStore.load(null);
199
200 SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
201 null);
202 cipher.init(Cipher.ENCRYPT_MODE, key);
203
204 return true;
205
206 } catch (KeyPermanentlyInvalidatedException e) {
207 return false;
208 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
209 throw new RuntimeException("Błąd inicjalizacji Ciphera", e);
210 }
211
212 }
213 private Cipher getCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
214 return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
215 + KeyProperties.BLOCK_MODE_CBC + "/"
216 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
217 }
218
219
220
221}