-
Notifications
You must be signed in to change notification settings - Fork 6
/
ProfileExchanger.java
353 lines (275 loc) · 14 KB
/
ProfileExchanger.java
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package tud.cnlab.wifriends;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import tud.cnlab.wifriends.castle.AESCastle;
import tud.cnlab.wifriends.datahandlers.MdlFriendListDbHandler;
import tud.cnlab.wifriends.datahandlers.MdlProfileDbHandler;
import tud.cnlab.wifriends.datahandlers.TblMyProfile;
/**
* The Class which manages the Exchange of Profile information and the Photo albums
*/
public class ProfileExchanger implements Runnable {
private static final String TAG = "ProfileExchanger";
String passPhrase = "Dick Beck";
char[] passArray = passPhrase.toCharArray();
MdlFriendListDbHandler oFriendInfo;
AESCastle aesCastle;
static MdlProfileDbHandler profileDbHandleRead;
static TblMyProfile profileRead;
static MdlProfileDbHandler profileJsonWrite;
static TblMyProfile profileEdit;
String friendPath;
Context context = WiFriendsService.wf.getApplicationContext();
private Socket socket = null;
private Handler handler;
private OutputStream oStream;
public ProfileExchanger(Socket socket, Handler handler, MdlFriendListDbHandler oFriendInfo) {
this.socket = socket;
this.handler = handler;
this.oFriendInfo = oFriendInfo;
}
@Override
public void run() {
try {
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
DataInputStream dis = new DataInputStream(bis);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); //BOS
DataOutputStream dos = new DataOutputStream(bos); //DOS
friendPath = WiFriendsService.WIFRIENDS_PATH + "/" + WiFriendsService.FRIENDS_FOLDER + "/" + oFriendInfo.getUSER_MAC();
try {
/*******************Encrypting My Profile JSON**********************/
Log.d(TAG, "Starting Encryption of JSON");
byte[] myProfile;
profileDbHandleRead = new MdlProfileDbHandler();
profileRead = new TblMyProfile(context);
//profileRead = TblMyProfile.getInstance();
profileDbHandleRead = profileRead.retrieveMyProfile();
String JSON = createJSON(profileDbHandleRead);
System.out.println("Before Encryption \n Priniting AESKEYobject : " + oFriendInfo);
myProfile = AESCastle.EncryptProfile(JSON.getBytes("UTF-8"), oFriendInfo.getAES_KEY());
System.out.println("Length after Encryption:" + myProfile.length);
//sendMyProfile(myProfile);
String myEncrptPath = WiFriendsService.WIFRIENDS_PATH + "/" +
WiFriendsService.MYPROFILE_FOLDER + "/temp/" +
"MyProfile";
FileOutputStream fos = new FileOutputStream(myEncrptPath);
fos.write(myProfile);
fos.close();
Log.d(TAG, "Encrypted File written to local My Profile Folder");
/************************Encrypting Images***************************/
String myPath = WiFriendsService.WIFRIENDS_PATH + "/" + WiFriendsService.MYPROFILE_FOLDER;
File[] files = new File(myPath).listFiles();
try {
for (File file : files) {
if (!file.isDirectory()) {
String name = file.getName();
System.out.println("File name being Encrypted:" + name);
//if (!"MyProfile".equals(name)) { // All other image files
byte[] bFile = new byte[(int) file.length()];
byte[] bEncFile;
FileInputStream fis = new FileInputStream(file);
fis.read(bFile); // Image file is read in bFile as Bytes
fis.close();
bEncFile = AESCastle.EncryptProfile(bFile, oFriendInfo.getAES_KEY());
System.out.println("Length of image after Encryption:" + bEncFile.length);
String myImagePath = WiFriendsService.WIFRIENDS_PATH + "/" +
WiFriendsService.MYPROFILE_FOLDER + "/temp/" + name;
FileOutputStream fos1 = new FileOutputStream(myImagePath);
fos1.write(bEncFile);
fos1.close();
Log.d(TAG, "Encrypted File written to local temp folder with same name");
}
}
profileRead.close();
} catch (Exception e) {
Log.d(TAG, "Error in encrypting image files");
e.printStackTrace();
}
/**
* Sending Encrypted Files*
*
*/
String sendPath = WiFriendsService.WIFRIENDS_PATH + "/" +
WiFriendsService.MYPROFILE_FOLDER + "/temp/";
File[] sendFiles = new File(sendPath).listFiles();
System.out.println(sendFiles.length);
dos.writeInt(sendFiles.length);
Log.d(TAG, "No. of files to be sent: " + sendFiles.length);
try {
for (File file : sendFiles) {
// Write Individual File Length using DOS
long length = file.length();
dos.writeLong(length);
System.out.println("Length of encrypted file being sent:" + length);
// Write Individual File name using DOS - as UTF
String name = file.getName();
dos.writeUTF(name);
System.out.println("Name of encrypted file being sent:" + name);
FileInputStream fis = new FileInputStream(file); //FIS
BufferedInputStream bisLocal = new BufferedInputStream(fis); //BIS
int theByte = 0;
// Write the actual file using BOS
while ((theByte = bisLocal.read()) != -1) bos.write(theByte);
Log.d(TAG, "Sending a file...");
bisLocal.close();
fis.close();
}
dos.flush();
bos.flush();
} catch (Exception e) {
Log.d(TAG, "Error in sending encrypted files");
}
Log.d(TAG, "All files sent successfully");
// Sending part of temp directory Ends!!
} catch (Exception e) {
Log.d(TAG, "Error in Encrypting my profile / sending files");
e.printStackTrace();
}
System.out.println("Sent all files. Now Trying to READ");
/***********************Receiving Friends Encrypted files begin *********************************/
try {
File friendFile = new File(friendPath);
if (!friendFile.exists()) {
friendFile.mkdirs();
}
} catch (Exception e) {
Log.d(TAG, "Error in Directory creation for the friend!");
e.printStackTrace();
}
try {
Log.d(TAG, "Trying to Read the File Count...");
System.out.println("Checking for Data input Stream: "+dis.available());
int filesCount = dis.readInt();
Log.d(TAG, "No. of files to be received from the Socket: " + filesCount);
File[] files = new File[filesCount];
for (int i = 0; i < filesCount; i++) {
Log.d(TAG, "Inside For loop: " + i + " th iteration");
long fileLength = dis.readLong();
String fileName = dis.readUTF();
files[i] = new File(friendPath + "/" + fileName);
FileOutputStream fos = new FileOutputStream(files[i]);
BufferedOutputStream bosLocal = new BufferedOutputStream(fos);
for (int j = 0; j < fileLength; j++) bosLocal.write(bis.read());
bosLocal.close();
}
} catch (Exception e) {
Log.d(TAG, "Error in Receiving");
e.printStackTrace();
}
Log.d(TAG, "Read all incoming files");
/*************Reading data code ends*************************************************************************/
} catch (IOException e) {
Log.d("MASTER", "MASTER Error");
e.printStackTrace();
} finally {
try {
socket.close();
System.out.println("Trying to Decrypt the Profile and Images after closing the Socket");
/*************Decryption of Received Files begin*************************/
File[] decryptFiles = new File(friendPath).listFiles();
Log.d(TAG, "Decryption of received files begins");
for (File file : decryptFiles) {
// Write Individual File name using DOS - as UTF
String name = file.getName();
if (!"MyProfile".equals(name)) {
//Decrypt Images
byte[] bEncFile = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bEncFile);
fis.close();
byte[] bFile = AESCastle.DecryptProfile(bEncFile, oFriendInfo.getAES_KEY());
System.out.println("Length of image after Decryption:" + bFile.length);
if (friendPath.isEmpty() || friendPath == null) {
System.out.println("Friend path isnull");
}
if (name.isEmpty() || name == null) {
System.out.println("Name isnull");
}
String friendImagePath = friendPath + "/" + name;
FileOutputStream fos1 = new FileOutputStream(friendImagePath);
fos1.write(bFile);
fos1.close();
Log.d(TAG, "Decrypted File written to local Friend Profile Folder with same name");
} else {
byte[] bEncFile = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bEncFile);
fis.close();
persistFriendsProfile(bEncFile);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Method to save the Received friends profile*
* @param bEncFile
*/
public void persistFriendsProfile(byte[] bEncFile) {
String decryptedJSONProfile;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead = -1;
try {
byte[] profileToDecrypt = bEncFile;
System.out.println("Length received for Decryption: " + profileToDecrypt.length);
byte[] decryptedProfile = AESCastle.DecryptProfile(profileToDecrypt, oFriendInfo.getAES_KEY());
decryptedJSONProfile = new String(decryptedProfile, "UTF-8");
decryptedJSONProfile = decryptedJSONProfile.trim();
System.out.println("**************************");
System.out.println("Received JSON before Parsing: \n " + decryptedJSONProfile);
parseJSON(decryptedJSONProfile);
profileEdit = new TblMyProfile(context);
//profileEdit = TblMyProfile.getInstance();
profileEdit.updateProfile(profileJsonWrite);
Log.d(TAG, "Friend Profile Saved Successfully");
profileEdit.close();
} catch (Exception e) {
Log.d(TAG, "Error during Decryption:");
e.printStackTrace();
}
}
/**
* Create JSON file of the own profile
* @param profileJsonWrite
* @return
*/
public String createJSON(MdlProfileDbHandler profileJsonWrite) {
GsonBuilder profileJSON = new GsonBuilder();
profileJSON.setPrettyPrinting().serializeNulls();
Gson gson = profileJSON.create();
String JSON = gson.toJson(profileJsonWrite);
System.out.println("Printing the JSON");
System.out.println(JSON);
return JSON;
}
/**
* Parse the JSON using Google GSON to save it in DB*
* @param JSON
*/
public void parseJSON(String JSON) {
GsonBuilder profileJSON = new GsonBuilder();
profileJSON.setPrettyPrinting().serializeNulls();
Gson gson = profileJSON.create();
//Gson gson = new Gson();
//JsonReader.setLenient(true);
profileJsonWrite = gson.fromJson(JSON, MdlProfileDbHandler.class);
System.out.println("Printing the Object");
System.out.println(profileJsonWrite);
}
}