-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBEncryptFile.java
More file actions
164 lines (153 loc) · 5.21 KB
/
DBEncryptFile.java
File metadata and controls
164 lines (153 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.example.jay_pc.dcrypt;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* Created by Jay-pc on 3/10/2017.
*/
public class DBEncryptFile {
SecretKey secretKey = null;
Cipher cipher = null;
public DBEncryptFile(String masterPassword) {
try {
/**
* Create a Blowfish key
*/
secretKey = new SecretKeySpec(masterPassword.getBytes(), "Blowfish");
/**
* Create an instance of cipher mentioning the name of algorithm
* - Blowfish
*/
cipher = Cipher.getInstance("Blowfish");
} catch (NoSuchPaddingException ex) {
System.out.println(ex);
Log.e("DCrypt", ex.getMessage());
} catch (NoSuchAlgorithmException ex) {
Log.e("DCrypt", ex.getMessage());
}
}
/**
*
* @param srcPath
* @param destPath
*
* Encrypts the file in srcPath and creates a file in destPath
* @throws Exception
*/
protected void encrypt(String srcPath, String destPath) throws Exception {
File rawFile = new File(srcPath);
File encryptedFile = new File(destPath);
InputStream inStream = null;
OutputStream outStream = null;
try {
/**
* Initialize the cipher for encryption
*/
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
/**
* Initialize input and output streams
*/
inStream = new FileInputStream(rawFile);
outStream = new FileOutputStream(encryptedFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
outStream.write(cipher.doFinal());
inStream.close();
outStream.close();
} catch (Exception ex) {
Log.e("DCrypt", ex.getMessage());
throw ex;
}
}
/**
*
* @param srcPath
* @param destPath
*
* Decrypts the file in srcPath and creates a file in destPath
* @throws Exception
*/
protected void decrypt(String srcPath, String destPath) throws Exception {
File encryptedFile = new File(srcPath);
File decryptedFile = new File(destPath);
InputStream inStream = null;
OutputStream outStream = null;
try {
/**
* Initialize the cipher for decryption
*/
cipher.init(Cipher.DECRYPT_MODE, secretKey);
/**
* Initialize input and output streams
*/
inStream = new FileInputStream(encryptedFile);
outStream = new FileOutputStream(decryptedFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
outStream.write(cipher.doFinal());
inStream.close();
outStream.close();
} catch (Exception ex) {
Log.e("DCrypt", ex.getMessage());
throw ex;
}
}
/**
*
* @param filePath
*
* Shreds a file
*/
protected boolean shredFile(String filePath) {
File file = new File(filePath);
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
FileChannel fileChannel = randomAccessFile.getChannel();
ByteBuffer buffer;
try {
int numBytes = (int) fileChannel.size();
for (int i = 0; numBytes > 0; numBytes = numBytes - 1024, i++) {
byte[] randomBytes = new byte[1024];
buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, i * 1024, randomBytes.length);
buffer.clear();
new Random().nextBytes(randomBytes);
buffer.put(randomBytes);
fileChannel.write(buffer);
}
} finally {
fileChannel.close();
randomAccessFile.close();
System.gc();
Log.e("DCrypt", "File handles closed!");
}
} catch (FileNotFoundException exp) {
Log.e("DCrypt", exp.getMessage());
} catch (IOException exp) {
Log.e("DCrypt", exp.getMessage());
}
boolean status = file.delete();
return status;
}
}