· 5 years ago · Dec 02, 2019, 08:52 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
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 cipher3.doFinal("artur".getBytes());
121 } catch (InvalidKeyException e) {
122 e.printStackTrace();
123 } catch (BadPaddingException e) {
124 e.printStackTrace();
125 } catch (IllegalBlockSizeException e) {
126 e.printStackTrace();
127 }
128
129
130 }
131 });
132
133 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
134
135 fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
136 keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
137
138 if(!fingerprintManager.isHardwareDetected()){
139
140 mParaLabel.setText("Nie odnaleziono skanera w urządzeniu");
141
142 } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED){
143
144 mParaLabel.setText("Brak uprawnień do skanera");
145
146 } else if (!keyguardManager.isKeyguardSecure()){
147
148 mParaLabel.setText("Dodaj blokade w telefonie");
149
150 } else if (!fingerprintManager.hasEnrolledFingerprints()){
151
152 mParaLabel.setText("Dodaj przynajmniej jeden odcisk palca");
153
154 } else {
155
156 mParaLabel.setText("Umiesc palec w celu weryfikacaji");
157
158 SharedPreferences FingerContent = getSharedPreferences("test", 0);
159 String test = FingerContent.getString("test", "");
160 if (test == "") {
161 Toast.makeText(MainActivity.this, "Witam", Toast.LENGTH_SHORT).show();
162 generateKey();
163 SharedPreferences.Editor editor = FingerContent.edit();
164 editor.putString("test", "siema");
165 editor.apply();
166 }
167
168 if (cipherInit()){
169
170 FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
171 FingerprintHandler fingerprintHandler = new FingerprintHandler(this);
172 fingerprintHandler.startAuth(fingerprintManager, cryptoObject);
173 }
174 }
175
176 }
177
178 }
179
180 @TargetApi(Build.VERSION_CODES.M)
181 private void generateKey() {
182 try {
183
184 keyStore = KeyStore.getInstance("AndroidKeyStore");
185 KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
186
187 keyStore.load(null);
188 keyGenerator.init(new
189 KeyGenParameterSpec.Builder(KEY_NAME,
190 KeyProperties.PURPOSE_ENCRYPT |
191 KeyProperties.PURPOSE_DECRYPT)
192 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
193 .setUserAuthenticationRequired(true)
194 .setEncryptionPaddings(
195 KeyProperties.ENCRYPTION_PADDING_PKCS7)
196 .build());
197 keyGenerator.generateKey();
198
199 } catch (KeyStoreException | IOException | CertificateException
200 | NoSuchAlgorithmException | InvalidAlgorithmParameterException
201 | NoSuchProviderException e) {
202
203 e.printStackTrace();
204
205 }
206
207 }
208 @TargetApi(Build.VERSION_CODES.M)
209 public boolean cipherInit() {
210 try {
211 cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
212 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
213 throw new RuntimeException("Błąd Ciphera", e);
214 }
215
216 try {
217
218 keyStore.load(null);
219
220 SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
221 null);
222 cipher.init(Cipher.ENCRYPT_MODE, key);
223
224 return true;
225
226 } catch (KeyPermanentlyInvalidatedException e) {
227 return false;
228 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
229 throw new RuntimeException("Błąd inicjalizacji Ciphera", e);
230 }
231
232 }
233 private Cipher getCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
234 return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
235 + KeyProperties.BLOCK_MODE_CBC + "/"
236 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
237 }
238
239
240
241}