· 5 years ago · Feb 06, 2020, 10:38 AM
1private static final String PASSWORD = "PASSWORD";
2 private static final String ALGORITHM = "AES";
3 private static final String SECRET_KEY_FACTORY_ALGORITHM = "PBKDF2WithHmacSHA1";
4 private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
5 private static final int KEY_LENGTH = 128;
6 private static final int ITERATION = 65536;
7
8 private Cipher encryptCipher;
9 private Cipher decryptCipher;
10 private RandomAccessFile fileToEncrypt;
11 private OutputStream outFile;
12 private byte[] salt;
13 private SecretKey secret;
14 private byte[] iv;
15
16
17 @Override
18 protected Boolean doInBackground(RandomAccessFile... filesFromUser){
19 fileToEncrypt = filesFromUser[0];
20 try {
21
22 outFile = new FileOutputStream(Folders.EXTERNAL_FOLDER_DIR + "/aFolder/encrypted.des");
23 encryptFile(PASSWORD);
24
25 //TODO below is for testing
26 decryptFile();
27
28
29 } catch (Exception e) {
30 e.printStackTrace();
31 }
32 return true;
33 }
34
35 private void encryptFile(String key) throws Exception {
36
37 this.makeSalt();
38 this.makeSecretKey(key);
39 this.makeCipher();
40 this.makeIV();
41
42 try {
43 byte[] bytesToEncrypt = new byte[(int) fileToEncrypt.length()];
44 fileToEncrypt.read(bytesToEncrypt);
45 byte[] cipheredBytes = encryptCipher.doFinal(bytesToEncrypt);
46 outFile.write(cipheredBytes);
47 }
48 catch (Exception e){
49 Log.i(TAG,e.toString());
50 }
51 finally {
52 fileToEncrypt.close();
53 outFile.close();
54 }
55
56 }
57
58 private void decryptFile() throws Exception {
59
60 this.makeCipherForDecrypt();
61
62
63 RandomAccessFile fileToDecrypt = new RandomAccessFile(Folders.EXTERNAL_FOLDER_DIR + "/aFolder/encrypted.des", "rw");
64 OutputStream outFile = new FileOutputStream(Folders.EXTERNAL_FOLDER_DIR + "/aFolder/original.jpg");
65 try {
66 byte[] bytesToDecrypt = new byte[(int) fileToDecrypt.length()];
67 fileToDecrypt.read(bytesToDecrypt);
68 byte[] decipheredBytes = decryptCipher.doFinal(bytesToDecrypt);
69 outFile.write(decipheredBytes);
70
71 }catch (Exception e){
72 Log.i(TAG,e.toString());
73
74 }finally {
75 fileToDecrypt.close();
76 outFile.close();
77 }
78
79 }
80
81 private void makeSalt() {
82 salt = new byte[8];
83 SecureRandom secureRandom = new SecureRandom();
84 secureRandom.nextBytes(salt);
85
86 }
87
88 private void makeSecretKey(String key) throws Exception {
89 SecretKeyFactory factory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_ALGORITHM);
90 KeySpec spec = new PBEKeySpec(key.toCharArray(),salt,ITERATION,KEY_LENGTH );
91 SecretKey tmp = factory.generateSecret(spec);
92 secret = new SecretKeySpec(tmp.getEncoded(),ALGORITHM);
93 }
94
95
96 private void makeCipher() throws Exception{
97 encryptCipher = Cipher.getInstance(TRANSFORMATION);
98 encryptCipher.init(Cipher.ENCRYPT_MODE,secret);
99
100 }
101
102
103 private void makeIV()throws Exception{
104 AlgorithmParameters params = encryptCipher.getParameters();
105 iv = params.getParameterSpec(IvParameterSpec.class).getIV();
106 }
107
108 private void makeCipherForDecrypt() throws Exception{
109 decryptCipher = Cipher.getInstance(TRANSFORMATION);
110 decryptCipher.init(Cipher.DECRYPT_MODE,secret, new IvParameterSpec(iv));
111
112 }
113
114}