Skip to content

Commit

Permalink
java, how does it work?
Browse files Browse the repository at this point in the history
  • Loading branch information
reality committed Mar 29, 2016
1 parent b50ec35 commit a158532
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 60 deletions.
Binary file not shown.
Binary file not shown.
Binary file modified android/app/build/outputs/apk/app-debug-unaligned.apk
Binary file not shown.
Binary file modified android/app/build/outputs/apk/app-debug.apk
Binary file not shown.
87 changes: 34 additions & 53 deletions android/app/src/main/java/rehab/reality/alsuti/Encrypter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ public class Encrypter {
AssetManager am;
String cjsString;
String cliString;
Context context;
File outputDir;
UploadActivity uploader;
String ext;
String password;
String fileName;

public Encrypter(UploadActivity uploader, Context context) throws IOException {
this.context = context;
public Encrypter(UploadActivity uploader, Context context, String fileName, String password) throws IOException {
this.uploader = uploader;
this.fileName = fileName;
this.password = password;
am = context.getAssets();
this.outputDir = context.getCacheDir();

Scanner cjsScanner = new Scanner(am.open("cryptojs.js"));
cjsString = cjsScanner.useDelimiter("\\A").next();
Expand All @@ -38,11 +42,15 @@ public Encrypter(UploadActivity uploader, Context context) throws IOException {
Scanner cliScanner = new Scanner(am.open("encrypt_file.js"));
cliString = cliScanner.useDelimiter("\\A").next();
cliScanner.close();

}

public void encryptFile(String fileName, String password) throws IOException, JSException {
byte[] b = FileUtils.readFileToByteArray(new File(fileName));
public void runEncryption() {
byte[] b = new byte[0];
try {
b = FileUtils.readFileToByteArray(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}

String content = "YW5kcm9pZHN1Y2tz" + android.util.Base64.encodeToString(b, android.util.Base64.DEFAULT);

Expand All @@ -57,63 +65,36 @@ public void encryptFile(String fileName, String password) throws IOException, JS

ext = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

new RunEncryption(this, jsCode, password).execute();
Log.w("progress", "started encryption");
}
Log.w("progress", "built string");

JSContext js = new JSContext();
try {
js.evaluateScript(jsCode);
} catch (JSException e) {
e.printStackTrace();
}
Log.w("progress", "built ciphertext");
String ret = "";
try {
ret = js.property("result").toString();
} catch (JSException e) {
e.printStackTrace();
}
Log.w("progress", "got ciphertext");

void done(String cipherText) {
File outputDir = context.getCacheDir(); // context being the Activity pointer
File outputFile = null;
try {
outputFile = File.createTempFile("alsutiTemp", "." + ext, outputDir);
PrintWriter writer = new PrintWriter(outputFile, "UTF-8");
writer.print(cipherText);
writer.print(ret);
writer.close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
Log.w("error", String.valueOf(e.getStackTrace()));
}
Log.w("progress", "written temp file");

Log.w("progress", "got the ciphertext and written!");

Log.w("progress", "done encryption");
uploader.doneEncryption(outputFile.getAbsolutePath());
}

private class RunEncryption extends AsyncTask <String, String, String> {
Encrypter encrypter;
String password;
String jsCode;

public RunEncryption(Encrypter enc, String jsCode, String password) {
encrypter = enc;
this.password = password;
this.jsCode = jsCode;
}

@Override
protected String doInBackground(String... params) {
JSContext js = new JSContext();
try {
js.evaluateScript(jsCode);
} catch (JSException e) {
e.printStackTrace();
}
String ret = "";
try {
ret = js.property("result").toString();
} catch (JSException e) {
e.printStackTrace();
}

Log.w("progress", "done encryption");
return ret;
}

@Override
protected void onPostExecute(String result) {

Log.w("progress", "calling callback");
encrypter.done(result);
}
}
}
19 changes: 12 additions & 7 deletions android/app/src/main/java/rehab/reality/alsuti/UploadActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public void onClickBtn(View v) throws IOException, JSException {
encrypted = true;
progressBox.setText("Encrypting...");
try {
encrypter = new Encrypter(this, getBaseContext());
encrypter.encryptFile(waitingFileName, password);
encrypter = new Encrypter(this, getBaseContext(), waitingFileName, password);
encrypter.runEncryption();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "unable to load things", Toast.LENGTH_LONG).show();
Expand All @@ -104,11 +104,16 @@ public void onClickBtn(View v) throws IOException, JSException {
}
}

public void doneEncryption(String result) {
EditText progressBox = (EditText) findViewById(R.id.editText);
waitingFileName = result;
progressBox.setText("Uploading...");
postFile();
public void doneEncryption(final String result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
EditText progressBox = (EditText) findViewById(R.id.editText);
waitingFileName = result;
progressBox.setText("Uploading...");
postFile();
}
});
}

@Override
Expand Down

0 comments on commit a158532

Please sign in to comment.