From 1de42b0bfb04621ef04c73e33f37ca4b03bc87a1 Mon Sep 17 00:00:00 2001 From: Dominik Date: Tue, 19 Jun 2012 01:17:58 +0300 Subject: [PATCH] symmetric encryption working again --- .../src/org/thialfihar/android/apg/Apg.java | 11 ++- .../android/apg/ApgApplication.java | 17 +++- .../android/apg/service/ApgService.java | 18 ++-- .../android/apg/ui/EditKeyActivity.java | 2 +- .../android/apg/ui/EncryptActivity.java | 91 +++++-------------- .../android/apg/ui/widget/SectionView.java | 2 +- 6 files changed, 61 insertions(+), 80 deletions(-) diff --git a/org_apg/src/org/thialfihar/android/apg/Apg.java b/org_apg/src/org/thialfihar/android/apg/Apg.java index 858cde9ff..66250b4d0 100644 --- a/org_apg/src/org/thialfihar/android/apg/Apg.java +++ b/org_apg/src/org/thialfihar/android/apg/Apg.java @@ -1304,7 +1304,7 @@ public class Apg { context.getString(R.string.error_noEncryptionKeysOrPassPhrase)); } - if (signatureKeyId != 0) { + if (signatureKeyId != -1) { signingKeyRing = getSecretKeyRing(signatureKeyId); signingKey = getSigningKey(signatureKeyId); if (signingKey == null) { @@ -1333,6 +1333,7 @@ public class Apg { if (encryptionKeyIds.length == 0) { // symmetric encryption + Log.d(Constants.TAG, "encryptionKeyIds length is 0 -> symmetric encryption"); cPk.addMethod(passPhrase.toCharArray()); } for (int i = 0; i < encryptionKeyIds.length; ++i) { @@ -1346,7 +1347,7 @@ public class Apg { PGPSignatureGenerator signatureGenerator = null; PGPV3SignatureGenerator signatureV3Generator = null; - if (signatureKeyId != 0) { + if (signatureKeyId != -1) { if (progress != null) progress.setProgress(R.string.progress_preparingSignature, 10, 100); if (forceV3Signature) { @@ -1373,7 +1374,7 @@ public class Apg { compressGen = new PGPCompressedDataGenerator(compression); bcpgOut = new BCPGOutputStream(compressGen.open(encryptOut)); } - if (signatureKeyId != 0) { + if (signatureKeyId != -1) { if (forceV3Signature) { signatureV3Generator.generateOnePassVersion(false).encode(bcpgOut); } else { @@ -1394,7 +1395,7 @@ public class Apg { InputStream in = data.getInputStream(); while ((n = in.read(buffer)) > 0) { pOut.write(buffer, 0, n); - if (signatureKeyId != 0) { + if (signatureKeyId != -1) { if (forceV3Signature) { signatureV3Generator.update(buffer, 0, n); } else { @@ -1410,7 +1411,7 @@ public class Apg { literalGen.close(); - if (signatureKeyId != 0) { + if (signatureKeyId != -1) { if (progress != null) progress.setProgress(R.string.progress_generatingSignature, 95, 100); if (forceV3Signature) { diff --git a/org_apg/src/org/thialfihar/android/apg/ApgApplication.java b/org_apg/src/org/thialfihar/android/apg/ApgApplication.java index 27b8b82cf..b56e3182d 100644 --- a/org_apg/src/org/thialfihar/android/apg/ApgApplication.java +++ b/org_apg/src/org/thialfihar/android/apg/ApgApplication.java @@ -1,9 +1,24 @@ +/* + * Copyright (C) 2012 Dominik Schürmann + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.thialfihar.android.apg; import org.thialfihar.android.apg.passphrase.PassphraseCacheService; import android.app.Application; -import android.content.Intent; public class ApgApplication extends Application { diff --git a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java index f1ac46256..c36d814ba 100644 --- a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java +++ b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java @@ -77,7 +77,7 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { // generate key public static final String ALGORITHM = "algorithm"; public static final String KEY_SIZE = "key_size"; - public static final String PASSPHRASE = "passphrase"; + public static final String SYMMETRIC_PASSPHRASE = "passphrase"; public static final String MASTER_KEY = "master_key"; // encrypt @@ -182,7 +182,7 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { try { // Input int algorithm = data.getInt(ALGORITHM); - String passphrase = data.getString(PASSPHRASE); + String passphrase = data.getString(SYMMETRIC_PASSPHRASE); int keysize = data.getInt(KEY_SIZE); PGPSecretKey masterKey = null; if (data.containsKey(MASTER_KEY)) { @@ -206,7 +206,7 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { case ACTION_GENERATE_DEFAULT_RSA_KEYS: // generate one RSA 2048 key for signing and one subkey for encrypting! try { - String passphrase = data.getString(PASSPHRASE); + String passphrase = data.getString(SYMMETRIC_PASSPHRASE); // Operation PGPSecretKeyRing masterKeyRing = Apg.createKey(this, Id.choice.algorithm.rsa, 2048, @@ -232,7 +232,7 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { try { // Input long secretKeyId = data.getLong(SECRET_KEY_ID); - String passphrase = data.getString(PASSPHRASE); + String passphrase = data.getString(SYMMETRIC_PASSPHRASE); byte[] bytes = data.getByteArray(BYTES); @@ -251,16 +251,19 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); if (generateSignature) { + Log.d(Constants.TAG, "generate signature..."); Apg.generateSignature(this, inputData, outStream, useAsciiArmour, false, secretKeyId, Apg.getCachedPassPhrase(secretKeyId), Preferences .getPreferences(this).getDefaultHashAlgorithm(), Preferences .getPreferences(this).getForceV3Signatures(), this); } else if (signOnly) { + Log.d(Constants.TAG, "sign only..."); Apg.signText(this, inputData, outStream, secretKeyId, Apg .getCachedPassPhrase(secretKeyId), Preferences.getPreferences(this) .getDefaultHashAlgorithm(), Preferences.getPreferences(this) .getForceV3Signatures(), this); } else { + Log.d(Constants.TAG, "encrypt..."); Apg.encrypt(this, inputData, outStream, useAsciiArmour, encryptionKeyIds, signatureKeyId, Apg.getCachedPassPhrase(signatureKeyId), this, Preferences.getPreferences(this).getDefaultEncryptionAlgorithm(), @@ -301,7 +304,7 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { try { // Input long secretKeyId = data.getLong(SECRET_KEY_ID); - String passphrase = data.getString(PASSPHRASE); + String passphrase = data.getString(SYMMETRIC_PASSPHRASE); String inputFile = data.getString(INPUT_FILE); String outputFile = data.getString(OUTPUT_FILE); @@ -343,16 +346,19 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { // Operation if (generateSignature) { + Log.d(Constants.TAG, "generate signature..."); Apg.generateSignature(this, inputData, outStream, useAsciiArmour, true, secretKeyId, Apg.getCachedPassPhrase(secretKeyId), Preferences .getPreferences(this).getDefaultHashAlgorithm(), Preferences .getPreferences(this).getForceV3Signatures(), this); } else if (signOnly) { + Log.d(Constants.TAG, "sign only..."); Apg.signText(this, inputData, outStream, secretKeyId, Apg .getCachedPassPhrase(secretKeyId), Preferences.getPreferences(this) .getDefaultHashAlgorithm(), Preferences.getPreferences(this) .getForceV3Signatures(), this); } else { + Log.d(Constants.TAG, "encrypt..."); Apg.encrypt(this, inputData, outStream, useAsciiArmour, encryptionKeyIds, signatureKeyId, Apg.getCachedPassPhrase(signatureKeyId), this, Preferences.getPreferences(this).getDefaultEncryptionAlgorithm(), @@ -373,7 +379,7 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { try { // Input long secretKeyId = data.getLong(SECRET_KEY_ID); - String passphrase = data.getString(PASSPHRASE); + String passphrase = data.getString(SYMMETRIC_PASSPHRASE); Uri providerUri = Uri.parse(data.getString(PROVIDER_URI)); diff --git a/org_apg/src/org/thialfihar/android/apg/ui/EditKeyActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/EditKeyActivity.java index dd62bc7e5..ab3956abc 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/EditKeyActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/EditKeyActivity.java @@ -192,7 +192,7 @@ public class EditKeyActivity extends SherlockFragmentActivity { // fill values for this action Bundle data = new Bundle(); - data.putString(ApgService.PASSPHRASE, mCurrentPassPhrase); + data.putString(ApgService.SYMMETRIC_PASSPHRASE, mCurrentPassPhrase); intent.putExtra(ApgService.EXTRA_DATA, data); diff --git a/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java index 5848fdd2b..5801dd09c 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java @@ -158,21 +158,19 @@ public class EncryptActivity extends SherlockFragmentActivity { startActivity(intent); return true; - case Id.menu.option.encrypt_to_clipboard: { - Log.d(Constants.TAG, "encrypt_to_clipboard option item clicked!"); + case Id.menu.option.encrypt_to_clipboard: encryptToClipboardClicked(); return true; - } - case Id.menu.option.encrypt: { + + case Id.menu.option.encrypt: encryptClicked(); return true; - } - default: { + default: return super.onOptionsItemSelected(item); - } + } } @@ -726,46 +724,36 @@ public class EncryptActivity extends SherlockFragmentActivity { } - // @Override - // public void passPhraseCallback(long keyId, String passPhrase) { - // // super.passPhraseCallback(keyId, passPhrase); - // if (mEncryptTarget == Id.target.file) { - // askForOutputFilename(); - // } else { - // encryptStart(); - // } - // } - private void encryptStart() { - boolean useAsciiArmour = true; - long encryptionKeyIds[] = null; - long signatureKeyId = 0; - int compressionId = 0; - boolean signOnly = false; - - String passPhrase = null; - if (mMode.getCurrentView().getId() == R.id.modeSymmetric) { - passPhrase = mPassPhrase.getText().toString(); - if (passPhrase.length() == 0) { - passPhrase = null; - } - } else { - encryptionKeyIds = mEncryptionKeyIds; - signatureKeyId = getSecretKeyId(); - signOnly = (mEncryptionKeyIds == null || mEncryptionKeyIds.length == 0); - } - // Send all information needed to service to edit key in other thread Intent intent = new Intent(this, ApgService.class); // fill values for this action Bundle data = new Bundle(); + boolean useAsciiArmour = true; + long encryptionKeyIds[] = null; + long signatureKeyId = -1; // -1 means no signature! + int compressionId = 0; + boolean signOnly = false; + + if (mMode.getCurrentView().getId() == R.id.modeSymmetric) { + Log.d(Constants.TAG, "Symmetric encryption enabled!"); + String passPhrase = mPassPhrase.getText().toString(); + if (passPhrase.length() == 0) { + passPhrase = null; + } + + data.putString(ApgService.SYMMETRIC_PASSPHRASE, passPhrase); + } else { + encryptionKeyIds = mEncryptionKeyIds; + signatureKeyId = getSecretKeyId(); + signOnly = (mEncryptionKeyIds == null || mEncryptionKeyIds.length == 0); + } + // choose default settings, action and data bundle by target if (mContentUri != null) { - intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_ENCRYPT_SIGN_STREAM); - data.putString(ApgService.PROVIDER_URI, mContentUri.toString()); } else if (mEncryptTarget == Id.target.file) { @@ -795,7 +783,6 @@ public class EncryptActivity extends SherlockFragmentActivity { } data.putByteArray(ApgService.BYTES, message.getBytes()); } - } if (mOverrideAsciiArmour) { @@ -1034,32 +1021,4 @@ public class EncryptActivity extends SherlockFragmentActivity { super.onActivityResult(requestCode, resultCode, data); } - // @Override - // protected Dialog onCreateDialog(int id) { - // switch (id) { - // case Id.dialog.output_filename: { - // return FileDialog.build(this, getString(R.string.title_encryptToFile), - // getString(R.string.specifyFileToEncryptTo), mOutputFilename, - // new FileDialog.OnClickListener() { - // public void onOkClick(String filename, boolean checked) { - // removeDialog(Id.dialog.output_filename); - // mOutputFilename = filename; - // encryptStart(); - // } - // - // public void onCancelClick() { - // removeDialog(Id.dialog.output_filename); - // } - // }, getString(R.string.filemanager_titleSave), - // getString(R.string.filemanager_btnSave), null, Id.request.output_filename); - // } - // - // default: { - // break; - // } - // } - // - // return super.onCreateDialog(id); - // } - } diff --git a/org_apg/src/org/thialfihar/android/apg/ui/widget/SectionView.java b/org_apg/src/org/thialfihar/android/apg/ui/widget/SectionView.java index c12156355..98532a00b 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/widget/SectionView.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/widget/SectionView.java @@ -265,7 +265,7 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor } else { passPhrase = ""; } - data.putString(ApgService.PASSPHRASE, passPhrase); + data.putString(ApgService.SYMMETRIC_PASSPHRASE, passPhrase); data.putInt(ApgService.ALGORITHM, mNewKeyAlgorithmChoice.getId()); data.putInt(ApgService.KEY_SIZE, mNewKeySize);