-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCryptActivity.java
More file actions
507 lines (419 loc) · 16.8 KB
/
DCryptActivity.java
File metadata and controls
507 lines (419 loc) · 16.8 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package com.example.jay_pc.dcrypt;
/**
* Created by Jay-pc on 3/10/2017.
*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
//import android.support.v4.content.CursorLoader;
public class DCryptActivity extends Activity {
String dcryptPref = "com.example.jay_pc.dcrypt";
private String fileShreddingPref = "SHREDDING";
private Button startButton = null;
Button stopButton = null;
private Dialog helpDialog = null;
private Dialog logDialog = null;
//private GridView dbGridView = null;
private TextView dbLogTextView = null;
private String prevPicId, nextPicId;
private String masterPassword = null;
private AlertDialog.Builder passwordDialog = null;
SharedPreferences dPref = null;
private static boolean doShredding = false;
private static com.example.jay_pc.dcrypt.DBLogger Log = null;
//private Menu optionsMenu = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log = new DBLogger(this);
startButton = (Button) findViewById(R.id.startButton);
stopButton = (Button) findViewById(R.id.stopButton);
//dbGridView = (GridView) findViewById(R.id.dCryptGridView);
dbLogTextView = (TextView) findViewById(R.id.dbMainTextView);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
startService(new Intent(DCryptActivity.this, DCryptService.class));
}
});
stopButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(DCryptActivity.this, DCryptService.class));
finish();
}
});
dPref = getSharedPreferences(dcryptPref, MODE_PRIVATE);
doShredding = dPref.getBoolean(fileShreddingPref, false);
if (Intent.ACTION_MAIN.equals(getIntent().getAction())) {
handleIntents();
} else {
acceptPasswordFromUser();
}
//setUpUncaughtHandler();
}
private void handleIntents() {
Intent dbIntent = getIntent();
String dbIntentAction = dbIntent.getAction();
if (Intent.ACTION_SEND.equals(dbIntentAction)) {
handleSendFile(dbIntent);
} else if (Intent.ACTION_SEND_MULTIPLE.equals(dbIntentAction)) {
handleSendMultipleFiles(dbIntent);
} else if (Intent.ACTION_MAIN.equals(dbIntentAction)){
initComponents();
//toastAlert("main");
}
}
private void handleSendMultipleFiles(Intent intent) {
ArrayList<Uri> fileUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (fileUris != null) {
//toastAlert("Received " + fileUris.size() + " files!");
for (Uri uri : fileUris) {
File file = new File(getPath(uri));
workOnOneFile(file);
}
}
}
private void handleSendFile(Intent intent) {
//String fileType = intent.getType();
Uri fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
//toastAlert(fileUri + " received!");
File file = null;
file = new File(getPath(fileUri));
workOnOneFile(file);
}
private void workOnOneFile(File file) {
if (file.getName().endsWith(".dbx")) {
if (decryptFile(file)) {
toastAlert("Decrypted " + file.getAbsolutePath());
} else {
toastAlert("Decryption failed for " + file.getAbsolutePath());
}
} else {
if (encryptFile(file)) {
toastAlert("Encrypted " + file.getAbsolutePath());
} else {
toastAlert("Encryption failed for " + file.getAbsolutePath());
}
}
}
private boolean decryptFile(File file) {
String prefix = "";
DBEncryptFile dbEncryptFile = new DBEncryptFile(masterPassword);
String srcFileName = file.getAbsolutePath();
String dir = file.getParent();
String destFileName = prefix + file.getName().replaceAll(".dbx", "");
destFileName = dir + "/" + destFileName;
if (new File(destFileName).exists()) {
toastAlert("Decrypted file already FOUND!");
Log.e("DCrypt", "Decrypted file " + destFileName + " already FOUND!");
return false;
}
try {
dbEncryptFile.decrypt(srcFileName, destFileName);
if (doShredding) {
if(dbEncryptFile.shredFile(srcFileName)) {
Log.e("DCrypt", "Shredded file " + srcFileName);
} else {
if (new File(srcFileName).delete()) {
Log.e("DCrypt", "Shredded file " + srcFileName);
} else {
Log.e("DCrypt", "Shredding of file " + srcFileName + " failed!");
}
}
}
return true;
} catch(Exception ex) {
new File(destFileName).delete();
return false;
}
}
private boolean encryptFile(File file) {
DBEncryptFile dbEncryptFile = new DBEncryptFile(masterPassword);
String srcFileName = file.getAbsolutePath();
String destFileName = srcFileName + ".dbx";
if (new File(destFileName).exists()) {
toastAlert("Encrypted file already FOUND!");
Log.e("DCrypt", "Encrypted file " + destFileName + " already FOUND!");
return false;
}
try {
dbEncryptFile.encrypt(srcFileName, destFileName);
if (doShredding) {
if(dbEncryptFile.shredFile(srcFileName)) {
Log.e("DCrypt", "Shredded file " + srcFileName);
} else {
if (new File(srcFileName).delete()) {
Log.e("DCrypt", "Shredded file " + srcFileName);
} else {
Log.e("DCrypt", "Shredding of file " + srcFileName + " failed!");
}
}
}
return true;
} catch(Exception ex) {
new File(destFileName).delete();
return false;
}
}
private void acceptPasswordFromUser() {
passwordDialog = new AlertDialog.Builder(this);
masterPassword = null;
passwordDialog.setTitle("DCrypt");
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.password_dialog, null);
passwordDialog.setView(view);
final EditText input1 = (EditText) view.findViewById(R.id.passwordEditText1);
final EditText input2 = (EditText) view.findViewById(R.id.passwordEditText2);
passwordDialog.setCancelable(false);
passwordDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
masterPassword = input1.getText().toString();
if (masterPassword == null || masterPassword.trim().equals("")) {
toastAlert("The password cannot be empty!");
acceptPasswordFromUser();
return;
} else {
//toastAlert(masterPassword);
//System.out.println(masterPassword);
if (masterPassword.equals(input2.getText().toString())) {
handleIntents();
} else {
toastAlert("Entered passwords do not match!");
acceptPasswordFromUser();
return;
}
}
}
});
passwordDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
masterPassword = null;
}
});
passwordDialog.show();
}
public String getPath(Uri uri) {
Log.i("DCrypt", uri.toString());
String fileName="unknown";//default fileName
Uri filePathUri = uri;
if (uri.getScheme().toString().compareTo("content")==0) {
String[] projection = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(getBaseContext(), uri, projection, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(projection[0]);
cursor.moveToFirst();
fileName = cursor.getString(column_index);
} else if (uri.getScheme().compareTo("file")==0) {
fileName = filePathUri.toString().replace("file://", "");
} else {
fileName = fileName + "_" + filePathUri.getLastPathSegment();
}
Log.i("DCrypt", fileName);
return fileName;
}
protected void initComponents() {
/*
dbGridView.setAdapter(new DCryptImageAdapter(this));
dbGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
prevPicId = DCryptImageAdapter.getPrevItemId(position).getAbsolutePath();
nextPicId = DCryptImageAdapter.getNextItemId(position).getAbsolutePath();
showImage(dbGridView.getAdapter().getItemId(position), position);
}
});
*/
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("DCrypt running..");
stringBuilder.append("\n");
if (doShredding) {
stringBuilder.append("File Shredding is ENABLED");
} else {
stringBuilder.append("File Shredding is DISABLED");
}
stringBuilder.append("\n");
dbLogTextView.setText(stringBuilder.toString());
}
protected void showImage(long itemId, int position) {
Intent pictureViewer = new Intent(this, DCryptPictureViewer.class);
pictureViewer.putExtra("currentPicId", DCryptImageAdapter.getCurrItemId(position));
pictureViewer.putExtra("picPosition", position);
pictureViewer.putExtra("prevPicId", prevPicId);
pictureViewer.putExtra("nextPicId", nextPicId);
startActivityForResult(pictureViewer, 0);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//this.optionsMenu = menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Editor editor;
switch (item.getItemId()) {
case R.id.action_settings:
Menu menu;
if (item.hasSubMenu()) {
menu = item.getSubMenu();
if (doShredding) {
((MenuItem) menu.getItem(0)).setChecked(true);
} else {
((MenuItem) menu.getItem(1)).setChecked(true);
}
}
return true;
case R.id.action_help:
menuHelp();
return true;
case R.id.action_logs:
dumpLogs();
return true;
case R.id.enableShredding:
if (item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
doShredding = true;
editor = dPref.edit();
editor.putBoolean(fileShreddingPref, true);
editor.commit();
if (doShredding) {
toastAlert("Enabled Shredding");
}
return true;
case R.id.disableShredding:
if (item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
doShredding = false;
editor = dPref.edit();
editor.putBoolean(fileShreddingPref, false);
editor.commit();
if (!doShredding) {
toastAlert("Disabled Shredding");
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
*
*/
private void menuHelp() {
helpDialog = new Dialog(this);
helpDialog.setTitle(R.string.help_title);
helpDialog.setOwnerActivity(DCryptActivity.this);
helpDialog.setContentView(R.layout.help_dialog);
helpDialog.show();
//toastAlert("Help");
}
@Override
public void onBackPressed() {
if (helpDialog != null && helpDialog.isShowing()) {
helpDialog.dismiss();
toastAlert("dismissed!");
}
if (logDialog != null && logDialog.isShowing()) {
logDialog.dismiss();
toastAlert("dismissed!");
}
moveTaskToBack(true);
}
void toastAlert(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
dbLogTextView.append(" *" + message + "\n");
}
public void copy(String srcPath, String destPath) throws IOException {
File src = new File(srcPath);
File dst = new File(destPath);
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
out.flush();
}
in.close();
out.close();
}
protected void dumpLogs() {
logDialog = new Dialog(this);
logDialog.setTitle(R.string.log_title);
logDialog.setOwnerActivity(DCryptActivity.this);
logDialog.setContentView(R.layout.log_dialog);
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("DCrypt") || line.contains("dcrypt")) {
log.append(line);
log.append("\n");
}
}
TextView tv = (TextView) logDialog.findViewById(R.id.logTextView);
tv.setText(log.toString());
} catch (IOException exp) {
Log.e("DCrypt", exp.getMessage());
}
logDialog.show();
}
public static void setUpUncaughtHandler() {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
UncaughtExceptionHandler defaultHandler;
{
defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("DCrypt", ex.getMessage());
defaultHandler.uncaughtException(thread, ex);
}
});
}
protected void appendToMainTextView(String message) {
dbLogTextView.append(" *" + message + "\n");
}
}