encryption working with a temporary hack (default account)

This commit is contained in:
Zeapo 2014-08-06 16:43:37 +01:00
parent d4380507b0
commit 42d22abfbc
4 changed files with 176 additions and 72 deletions

View file

@ -199,9 +199,9 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI
Intent intent = new Intent(this, PgpHandler.class); Intent intent = new Intent(this, PgpHandler.class);
intent.putExtra("PGP-ID", FileUtils.readFileToString(PasswordRepository.getFile("/.gpg-id"))); intent.putExtra("PGP-ID", FileUtils.readFileToString(PasswordRepository.getFile("/.gpg-id")));
intent.putExtra("NAME", "test.gpg"); intent.putExtra("NAME", "test.gpg");
intent.putExtra("FILE_PATH", this.currentDir.getAbsolutePath() + "/test.gpg"); intent.putExtra("FILE_PATH", this.currentDir.getAbsolutePath());
intent.putExtra("Operation", "ENCRYPT"); intent.putExtra("Operation", "ENCRYPT");
startActivity(intent); startActivityForResult(intent, 0);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -32,15 +32,20 @@ import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
import org.openintents.openpgp.util.OpenPgpUtils; import org.openintents.openpgp.util.OpenPgpUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
public class PgpHandler extends Activity { public class PgpHandler extends Activity {
private OpenPgpServiceConnection mServiceConnection; private OpenPgpServiceConnection mServiceConnection;
private String keyIDs = "";
SharedPreferences settings;
public static final int REQUEST_CODE_SIGN = 9910; public static final int REQUEST_CODE_SIGN = 9910;
public static final int REQUEST_CODE_ENCRYPT = 9911; public static final int REQUEST_CODE_ENCRYPT = 9911;
@ -73,7 +78,7 @@ public class PgpHandler extends Activity {
} }
// some persistance // some persistance
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); settings = PreferenceManager.getDefaultSharedPreferences(this);
String providerPackageName = settings.getString("openpgp_provider_list", ""); String providerPackageName = settings.getString("openpgp_provider_list", "");
String accountName = settings.getString("openpgp_account_name", ""); String accountName = settings.getString("openpgp_account_name", "");
@ -109,8 +114,20 @@ public class PgpHandler extends Activity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
public void decrypt(View view) { public void handleClick(View view) {
decryptAndVerify(new Intent()); switch (view.getId()) {
case R.id.crypto_show_button :
decryptAndVerify(new Intent());
break;
case R.id.crypto_confirm_add:
encrypt(new Intent());
break;
case R.id.crypto_cancel_add:
break;
default:
// should not happen
}
// getKeyIds(new Intent()); // getKeyIds(new Intent());
} }
@ -124,6 +141,7 @@ public class PgpHandler extends Activity {
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
Log.e(Constants.TAG, "onError getErrorId:" + error.getErrorId()); Log.e(Constants.TAG, "onError getErrorId:" + error.getErrorId());
Log.e(Constants.TAG, "onError getMessage:" + error.getMessage()); Log.e(Constants.TAG, "onError getMessage:" + error.getMessage());
Log.e(Constants.TAG, " " + error.toString());
} }
}); });
} }
@ -202,7 +220,7 @@ public class PgpHandler extends Activity {
showToast("RESULT_CODE_SUCCESS"); showToast("RESULT_CODE_SUCCESS");
// encrypt/decrypt/sign/verify // encrypt/decrypt/sign/verify
if (os != null) { if (requestCode == REQUEST_CODE_DECRYPT_AND_VERIFY && os != null) {
try { try {
Log.d(OpenPgpApi.TAG, "result: " + os.toByteArray().length Log.d(OpenPgpApi.TAG, "result: " + os.toByteArray().length
+ " str=" + os.toString("UTF-8")); + " str=" + os.toString("UTF-8"));
@ -227,6 +245,29 @@ public class PgpHandler extends Activity {
Log.e(Constants.TAG, "UnsupportedEncodingException", e); Log.e(Constants.TAG, "UnsupportedEncodingException", e);
} }
} }
// encrypt
if (requestCode == REQUEST_CODE_ENCRYPT && os != null) {
try {
Log.d(OpenPgpApi.TAG, "result: " + os.toByteArray().length
+ " str=" + os.toString("UTF-8"));
if (returnToCiphertextField) {
String path = getIntent().getExtras().getString("FILE_PATH")
+ "/" + ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString()
+ ".gpg";
OutputStream outputStream = FileUtils.openOutputStream(new File(path));
outputStream.write(os.toByteArray());
finishActivity(0);
} else {
showToast(os.toString());
}
} catch (Exception e) {
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
}
// verify // verify
if (result.hasExtra(OpenPgpApi.RESULT_SIGNATURE)) { if (result.hasExtra(OpenPgpApi.RESULT_SIGNATURE)) {
@ -237,13 +278,11 @@ public class PgpHandler extends Activity {
// get key ids // get key ids
if (result.hasExtra(OpenPgpApi.RESULT_KEY_IDS)) { if (result.hasExtra(OpenPgpApi.RESULT_KEY_IDS)) {
long[] keyIds = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS); long[] ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS);
String out = "keyIds: ";
for (int i = 0; i < keyIds.length; i++) {
out += OpenPgpUtils.convertKeyIdToHex(keyIds[i]) + ", ";
}
showToast(out); for (int i = 0; i < ids.length; i++) {
keyIDs += OpenPgpUtils.convertKeyIdToHex(ids[i]) + ", ";
}
} }
break; break;
} }
@ -288,6 +327,32 @@ public class PgpHandler extends Activity {
} }
public void encrypt(Intent data) {
data.setAction(OpenPgpApi.ACTION_ENCRYPT);
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[] {"default"});
data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
String pass = ((EditText) findViewById(R.id.crypto_password_edit)).getText().toString();
String extra = ((EditText) findViewById(R.id.crypto_extra_edit)).getText().toString();
ByteArrayInputStream is;
try {
is = new ByteArrayInputStream((pass + "\n" + extra).getBytes("UTF-8"));
ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(this, mServiceConnection.getService());
api.executeApiAsync(data, is, os, new MyCallback(true, os, REQUEST_CODE_ENCRYPT));
}catch (Exception e) {
e.printStackTrace();
}
}
public void getKeyIds(Intent data) { public void getKeyIds(Intent data) {
data.setAction(OpenPgpApi.ACTION_GET_KEY_IDS); data.setAction(OpenPgpApi.ACTION_GET_KEY_IDS);
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{getIntent().getExtras().getString("PGP-ID")}); data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{getIntent().getExtras().getString("PGP-ID")});
@ -314,10 +379,10 @@ public class PgpHandler extends Activity {
// sign(data); // sign(data);
// break; // break;
// } // }
// case REQUEST_CODE_ENCRYPT: { case REQUEST_CODE_ENCRYPT: {
// encrypt(data); encrypt(data);
// break; break;
// } }
// case REQUEST_CODE_SIGN_AND_ENCRYPT: { // case REQUEST_CODE_SIGN_AND_ENCRYPT: {
// signAndEncrypt(data); // signAndEncrypt(data);
// break; // break;

View file

@ -36,7 +36,7 @@
android:background="@android:drawable/screen_background_light_transparent" android:background="@android:drawable/screen_background_light_transparent"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:visibility="invisible" android:visibility="invisible"
android:onClick="decrypt" android:onClick="handleClick"
android:layout_column="2" android:layout_column="2"
android:layout_row="0"/> android:layout_row="0"/>
</GridLayout> </GridLayout>

View file

@ -1,84 +1,123 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="fill_parent"
android:layout_height="match_parent" android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.zeapo.pwdstore.crypto.PgpHandler"
android:orientation="vertical"
android:background="#eee"> android:background="#eee">
<LinearLayout <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="fill_parent"
android:background="@drawable/rectangle" tools:context="com.zeapo.pwdstore.crypto.PgpHandler">
android:orientation="horizontal">
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Name: "/>
<EditText
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_orange_dark"/>
</LinearLayout>
<LinearLayout
android:id="@+id/crypto_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@drawable/rectangle"
android:layout_height="wrap_content">
<LinearLayout <LinearLayout
android:id="@+id/crypto_password_edit_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:background="@drawable/rectangle"
android:orientation="horizontal"
android:layout_row="1"
android:layout_column="0">
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Name: "/>
<EditText
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/crypto_password_file_edit"
android:textColor="@android:color/holo_orange_dark"/>
</LinearLayout>
<LinearLayout
android:id="@+id/crypto_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@drawable/rectangle"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="0">
<LinearLayout <LinearLayout
android:id="@+id/crypto_password_edit_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Password: "/>
<EditText
android:id="@+id/crypto_password_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:typeface="monospace"/>
</LinearLayout>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textStyle="bold" android:textStyle="bold"
android:text="Password: "/> android:text="Extra content: "/>
<EditText <EditText
android:id="@+id/crypto_password_edit" android:id="@+id/crypto_extra_edit"
android:layout_width="fill_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:enabled="true"
android:visibility="visible"
android:typeface="monospace"/> android:typeface="monospace"/>
</LinearLayout> </LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textStyle="bold"
android:text="Extra content: "/>
<EditText
android:id="@+id/crypto_edit_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:visibility="visible"
android:typeface="monospace"/>
</LinearLayout> </LinearLayout>
</LinearLayout>
</LinearLayout> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0">
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@android:drawable/ic_input_add"
android:background="@drawable/oval"
android:id="@+id/crypto_confirm_add"
android:onClick="handleClick"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@android:drawable/ic_delete"
android:background="@drawable/oval"
android:id="@+id/crypto_cancel_add"
android:onClick="handleClick"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</GridLayout>
</ScrollView>