-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.java
More file actions
195 lines (157 loc) · 5.24 KB
/
Utilities.java
File metadata and controls
195 lines (157 loc) · 5.24 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Utilities {
public static SecretKey calculateUserKey(String user, String pass)
throws NoSuchAlgorithmException {
String keyPhrase = user + pass;
String keyHash = Utilities.sha1(keyPhrase);
byte[] keyHashBytes = keyHash.getBytes();
byte key[] = Arrays.copyOf(keyHashBytes, 16);
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
return secretKey;
}
public static int getAvailablePort() throws IOException {
int port = 0;
Random r = new Random();
do {
port = r.nextInt(20000) + 10000;
} while (!isPortAvailable(port));
return port;
}
private static boolean isPortAvailable(final int port) throws IOException {
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
return true;
} catch (final IOException e) {
} finally {
if (ss != null) {
ss.close();
}
}
return false;
}
/*
* Get SHA-1 hash of file name
*/
public static String sha1(String input) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16)
.substring(1));
}
return sb.toString();
}
public static void encrypt(String fname, String username, String password)
throws Exception {
// System.out.println("Encrypting " + fname + " with " + username + " "
// + "****");
SecretKey key = calculateUserKey(username, password);
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, key);
// creating file output stream to write to file
try (FileOutputStream fos = new FileOutputStream(fname + ".enc")) {
// creating output stream to write to file
ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(key);
// creating file input stream to read contents
try (FileInputStream fis = new FileInputStream(fname)) {
// creating cipher output stream to write encrypted contents
try (CipherOutputStream cos = new CipherOutputStream(fos,
aesCipher)) {
int read;
byte buf[] = new byte[4096];
while ((read = fis.read(buf)) != -1)
// Writing to file as cipher
cos.write(buf, 0, read);
}
}
}
}
public static void splitFile(String fileName, int parts) throws IOException {
try {
// System.out.println("Splitting file :" + fileName);
File f = new File(fileName);
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f));
FileOutputStream out;
String name = f.getName();
int partCounter = 1;
int sizeOfFiles = (int) (f.length() / parts) + 1;
byte[] buffer = new byte[sizeOfFiles];
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
String path = f.getParent() + "/" + name + "."
+ String.format("%03d", partCounter++);
File newFile = new File(path);
newFile.createNewFile();
out = new FileOutputStream(newFile);
out.write(buffer, 0, tmp);
out.close();
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
public static void join(String path, String baseFilename, int numberParts,
String newFileName) throws IOException {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(path + newFileName));
for (int part = 0; part < numberParts; part++) {
String partFile = String.format(path +"/" + baseFilename + "."
+ "%03d", part + 1);
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(partFile));
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}
out.close();
}
public static void decrypt(String fname, String username, String password,
String finalName) throws Exception {
String path = Peer.getDir() + Peer.hostName + "/ret/";
SecretKey key = calculateUserKey(username, password);
// System.out.println("Attempting to decrypt with " + username + " and "
// + password);
//System.out.println(fname);
// creating file input stream to read from file
try (FileInputStream fis = new FileInputStream(fname)) {
ObjectInputStream ois = new ObjectInputStream(fis);
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, key);
try (FileOutputStream fos = new FileOutputStream(path + finalName)) {
// creating cipher input stream to read encrypted contents
try (CipherInputStream cis = new CipherInputStream(fis,
aesCipher)) {
int read;
byte buf[] = new byte[4096];
while ((read = cis.read(buf)) != -1)
// Writing to file in Decrypt mode
fos.write(buf, 0, read);
}
}
}
}
}