· 6 years ago · May 02, 2019, 07:46 AM
1package sample.files;
2import javax.crypto.Cipher;
3import javax.crypto.KeyGenerator;
4import javax.crypto.SecretKey;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.nio.file.Files;
10import java.nio.file.Path;
11import java.nio.file.Paths;
12import java.security.SecureRandom;
13import java.util.List;
14import java.util.stream.Collectors;
15
16public abstract class FileUtils {
17
18private static final String UsedAlgorithm = "RC4";
19
20 public static void decrypt(File crypt, String out, String password, String extraExtensions, OnProcess onProcess) throws Exception{
21
22 String cryptName = crypt.getName();
23 String filename = cryptName.replace(".dec","");
24
25 FileInputStream fis = new FileInputStream(crypt);
26
27
28 // create a binary key from the argument key (seed)
29 SecureRandom sr = new SecureRandom(password.getBytes());
30 KeyGenerator kg = KeyGenerator.getInstance(UsedAlgorithm);
31 kg.init(sr);
32 SecretKey sk = kg.generateKey();
33
34 // do the decryption with that key
35 Cipher cipher = Cipher.getInstance(UsedAlgorithm);
36 cipher.init(Cipher.DECRYPT_MODE, sk);
37
38 StringBuilder path = new StringBuilder(out + filename);
39
40 if (extraExtensions.startsWith("."))
41 path.append(extraExtensions);
42 else
43 path.append(".").append(extraExtensions);
44
45
46
47 FileOutputStream fos = new FileOutputStream(path.toString());
48
49 byte[] in = new byte[64];
50 int read;
51
52 long processed = 0;
53
54 while ((read = fis.read(in)) != -1) {
55 byte[] output = cipher.update(in, 0, read);
56 if (output != null)
57 fos.write(output);
58
59 processed += in.length;
60
61 if (processed < crypt.length())
62 onProcess.run((int) (processed * 100 / crypt.length()));
63 }
64
65 byte[] output = cipher.doFinal();
66 if (output != null){
67 fos.write(output);
68 onProcess.run(100);
69 }
70
71 fis.close();
72 fos.flush();
73 fos.close();
74 }
75
76public static void encrypt(File in, String out, String password, OnProcess onProcess, boolean removeExtensions) throws Exception {
77 String filename;
78 if (removeExtensions)
79 filename = in.getName().substring(0, in.getName().lastIndexOf("."));
80
81 else
82 filename = in.getName();
83
84 String cryptName = filename + ".dec";
85
86 FileInputStream inFile = new FileInputStream(in);
87 FileOutputStream outFile = new FileOutputStream(new File(out +cryptName));
88
89
90 // create a binary key from the argument key (seed)
91 SecureRandom sr = new SecureRandom(password.getBytes());
92 KeyGenerator kg = KeyGenerator.getInstance(UsedAlgorithm);
93 kg.init(sr);
94 SecretKey sk = kg.generateKey();
95
96 // create an instance of cipher
97 Cipher cipher = Cipher.getInstance(UsedAlgorithm);
98
99 // initialize the cipher with the key
100 cipher.init(Cipher.ENCRYPT_MODE, sk);
101
102
103 byte[] buffer = new byte[64];
104
105 long processed = 0;
106
107 int bytesRead;
108 while ((bytesRead = inFile.read(buffer)) != -1) {
109 byte[] output = cipher.update(buffer, 0, bytesRead);
110 if (output != null) {
111 outFile.write(output);
112
113 processed += buffer.length;
114
115 if (processed < in.length())
116 onProcess.run((int) (processed * 100 / in.length()));
117 }
118 }
119
120 byte[] output = cipher.doFinal();
121 if (output != null) {
122 outFile.write(output);
123 onProcess.run(100);
124 }
125 inFile.close();
126 outFile.flush();
127 outFile.close();
128 }
129
130
131
132 public interface OnProcess {
133 void run(int progress);
134 }
135}