· 4 years ago · Jun 12, 2021, 10:20 AM
1Klasa potrzebna do inkrypcji i dekrypcji
2package com.example.notesh;
3
4
5import android.content.Context;
6import android.content.SharedPreferences;
7import android.os.Build;
8import android.security.keystore.KeyGenParameterSpec;
9import android.security.keystore.KeyProperties;
10import android.util.Base64;
11
12import androidx.annotation.RequiresApi;
13
14import java.io.IOException;
15import java.security.InvalidAlgorithmParameterException;
16import java.security.KeyStore;
17import java.security.KeyStoreException;
18import java.security.NoSuchAlgorithmException;
19import java.security.NoSuchProviderException;
20import java.security.UnrecoverableKeyException;
21import java.security.cert.CertificateException;
22
23import javax.crypto.Cipher;
24import javax.crypto.KeyGenerator;
25import javax.crypto.NoSuchPaddingException;
26import javax.crypto.SecretKey;
27import javax.crypto.spec.IvParameterSpec;
28
29
30public class SecretKeyUtils {
31
32 @RequiresApi(api = Build.VERSION_CODES.M)
33 public static void CheckAndGeneratePassword()
34 {
35 try {
36 SecretKey key = getSecretKey();
37 if (key == null)
38 {
39 generateSecretKey(new KeyGenParameterSpec.Builder(
40 "NoteHSecretKey",
41 KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
42 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
43 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
44 .build());
45 }
46 } catch (Exception e)
47 {
48 System.out.println("CheckAndGeneratePassword: " + e.toString());
49 }
50 }
51
52
53
54 @RequiresApi(api = Build.VERSION_CODES.M)
55 public static void generateSecretKey(KeyGenParameterSpec keyGenParameterSpec) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
56 KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
57 keyGenerator.init(keyGenParameterSpec);
58 keyGenerator.generateKey();
59 }
60
61 public static SecretKey getSecretKey() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException {
62 KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
63
64
65 keyStore.load(null);
66 return ((SecretKey)keyStore.getKey("NoteHSecretKey", null));
67 }
68
69 public static Cipher getCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
70 return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
71 + KeyProperties.BLOCK_MODE_CBC + "/"
72 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
73 }
74
75 public static String encryptNote(String note, Context context)
76 {
77 try {
78
79
80 Cipher cipher = getCipher();
81 SecretKey secretKey = getSecretKey();
82 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
83
84 byte[] iv = cipher.getIV();
85
86 SharedPreferences sharedPref = context.getSharedPreferences("szyfr", Context.MODE_PRIVATE);
87 SharedPreferences.Editor editor = sharedPref.edit();
88
89 editor.putString("ivi", Base64.encodeToString(iv, Base64.DEFAULT));
90 editor.commit();
91
92 byte[] encryptedInfo = cipher.doFinal(note.getBytes("UTF-8"));
93
94 return Base64.encodeToString(encryptedInfo, Base64.NO_WRAP);
95 }catch (Exception e)
96 {
97 System.out.println("Error while encrypting: " + e.toString());
98 }
99
100 return "";
101 }
102
103 public static String decryptNote(String note, Context context)
104 {
105 try {
106
107 SharedPreferences sharedPref = context.getSharedPreferences("note",Context.MODE_PRIVATE);
108 String ivArray = sharedPref.getString("ivi","");
109 byte[] iv = Base64.decode(ivArray, Base64.DEFAULT);
110
111 Cipher cipher = getCipher();
112 SecretKey secretKey = getSecretKey();
113 cipher.init(Cipher.DECRYPT_MODE, secretKey,new IvParameterSpec(iv));
114 return new String(cipher.doFinal(Base64.decode(note,Base64.NO_WRAP)));
115 }catch (Exception e)
116 {
117 System.out.println("Error while encrypting: " + e.toString());
118 }
119
120 return "";
121 }
122