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