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