steps towards symmetric file encryption

This commit is contained in:
Thialfihar 2010-04-23 00:01:59 +00:00
parent 78193007b2
commit ab6c884bdf
6 changed files with 158 additions and 17 deletions

View File

@ -49,6 +49,12 @@
</LinearLayout> </LinearLayout>
<CheckBox
android:id="@+id/ascii_armour"
android:text="@string/ascii_armour"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TabHost <TabHost
android:id="@+id/tab_host" android:id="@+id/tab_host"
android:layout_weight="1" android:layout_weight="1"
@ -66,6 +72,7 @@
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:paddingTop="65dip"> android:paddingTop="65dip">
<!-- -->
<LinearLayout <LinearLayout
android:id="@+id/tab_asymmetric" android:id="@+id/tab_asymmetric"
android:layout_width="fill_parent" android:layout_width="fill_parent"
@ -132,17 +139,82 @@
android:layout_height="0dip" android:layout_height="0dip"
android:layout_weight="1"/> android:layout_weight="1"/>
</LinearLayout> </LinearLayout>
<!-- -->
<LinearLayout <ScrollView
android:id="@+id/tab_symmetric" android:id="@+id/tab_symmetric"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"> android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="3dip">
<TableLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:stretchColumns="1"
android:layout_marginRight="?android:attr/scrollbarSize"
android:paddingLeft="6dip">
<TableRow
android:layout_marginBottom="5dip">
<TextView android:id="@+id/label_algorithm"
android:text="Algorithm:"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"/>
<Spinner
android:id="@+id/algorithm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/label_pass_phrase"
android:text="Pass phrase"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"/>
<EditText android:id="@+id/pass_phrase"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="textPassword"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/label_pass_phrase_again"
android:text="Again:"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"/>
<EditText android:id="@+id/pass_phrase_again"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="textPassword"/>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout> </ScrollView>
</FrameLayout> </FrameLayout>

View File

@ -83,5 +83,7 @@
<string name="filemanager_title_save">Save As...</string> <string name="filemanager_title_save">Save As...</string>
<string name="filemanager_btn_save">Save</string> <string name="filemanager_btn_save">Save</string>
<string name="ascii_armour">ASCII armour</string>
</resources> </resources>

View File

@ -1147,11 +1147,17 @@ public class Apg {
boolean armored, boolean armored,
long encryptionKeyIds[], long signatureKeyId, long encryptionKeyIds[], long signatureKeyId,
String signaturePassPhrase, String signaturePassPhrase,
ProgressDialogUpdater progress) ProgressDialogUpdater progress,
int symmetricAlgorithm,
String passPhrase)
throws IOException, GeneralException, PGPException, NoSuchProviderException, throws IOException, GeneralException, PGPException, NoSuchProviderException,
NoSuchAlgorithmException, SignatureException { NoSuchAlgorithmException, SignatureException {
Security.addProvider(new BouncyCastleProvider()); Security.addProvider(new BouncyCastleProvider());
if (encryptionKeyIds == null) {
encryptionKeyIds = new long[0];
}
ArmoredOutputStream armorOut = null; ArmoredOutputStream armorOut = null;
OutputStream out = null; OutputStream out = null;
OutputStream encryptOut = null; OutputStream encryptOut = null;
@ -1166,8 +1172,8 @@ public class Apg {
PGPSecretKeyRing signingKeyRing = null; PGPSecretKeyRing signingKeyRing = null;
PGPPrivateKey signaturePrivateKey = null; PGPPrivateKey signaturePrivateKey = null;
if (encryptionKeyIds == null || encryptionKeyIds.length == 0) { if (encryptionKeyIds.length == 0 && passPhrase == null) {
throw new GeneralException("no encryption key(s) given"); throw new GeneralException("no encryption key(s) or pass phrase given");
} }
if (signatureKeyId != 0) { if (signatureKeyId != 0) {
@ -1199,9 +1205,13 @@ public class Apg {
progress.setProgress("preparing streams...", 20, 100); progress.setProgress("preparing streams...", 20, 100);
// encryptFile and compress input file content // encryptFile and compress input file content
PGPEncryptedDataGenerator cPk = PGPEncryptedDataGenerator cPk =
new PGPEncryptedDataGenerator(PGPEncryptedData.AES_256, true, new SecureRandom(), new PGPEncryptedDataGenerator(symmetricAlgorithm, true, new SecureRandom(),
new BouncyCastleProvider()); new BouncyCastleProvider());
if (encryptionKeyIds.length == 0) {
// symmetric encryption
cPk.addMethod(passPhrase.toCharArray());
}
for (int i = 0; i < encryptionKeyIds.length; ++i) { for (int i = 0; i < encryptionKeyIds.length; ++i) {
PGPPublicKey key = getEncryptPublicKey(encryptionKeyIds[i]); PGPPublicKey key = getEncryptPublicKey(encryptionKeyIds[i]);
if (key != null) { if (key != null) {

View File

@ -31,12 +31,14 @@ import java.util.Collections;
import java.util.Vector; import java.util.Vector;
import org.bouncycastle2.bcpg.HashAlgorithmTags; import org.bouncycastle2.bcpg.HashAlgorithmTags;
import org.bouncycastle2.openpgp.PGPEncryptedData;
import org.bouncycastle2.openpgp.PGPException; import org.bouncycastle2.openpgp.PGPException;
import org.bouncycastle2.openpgp.PGPPublicKeyRing; import org.bouncycastle2.openpgp.PGPPublicKeyRing;
import org.bouncycastle2.openpgp.PGPSecretKey; import org.bouncycastle2.openpgp.PGPSecretKey;
import org.bouncycastle2.openpgp.PGPSecretKeyRing; import org.bouncycastle2.openpgp.PGPSecretKeyRing;
import org.openintents.intents.FileManager; import org.openintents.intents.FileManager;
import org.thialfihar.android.apg.Apg.GeneralException; import org.thialfihar.android.apg.Apg.GeneralException;
import org.thialfihar.android.apg.utils.Choice;
import android.app.Dialog; import android.app.Dialog;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
@ -47,11 +49,13 @@ import android.os.Environment;
import android.os.Message; import android.os.Message;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TabHost; import android.widget.TabHost;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -68,17 +72,24 @@ public class EncryptFileActivity extends BaseActivity {
private TextView mMainUserId = null; private TextView mMainUserId = null;
private TextView mMainUserIdRest = null; private TextView mMainUserIdRest = null;
private ListView mPublicKeyList = null; private ListView mPublicKeyList = null;
private Spinner mAlgorithm = null;
private EditText mPassPhrase = null;
private EditText mPassPhraseAgain = null;
private Button mAsciiArmour = null;
private Button mEncryptButton = null; private Button mEncryptButton = null;
private long mEncryptionKeyIds[] = null; private long mEncryptionKeyIds[] = null;
private String mInputFilename = null; private String mInputFilename = null;
private String mOutputFilename = null; private String mOutputFilename = null;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.encrypt_file); setContentView(R.layout.encrypt_file);
mAsciiArmour = (CheckBox) findViewById(R.id.ascii_armour);
mTabHost = (TabHost) findViewById(R.id.tab_host); mTabHost = (TabHost) findViewById(R.id.tab_host);
mTabHost.setup(); mTabHost.setup();
@ -96,6 +107,8 @@ public class EncryptFileActivity extends BaseActivity {
mTabHost.setCurrentTab(0); mTabHost.setCurrentTab(0);
// asymmetric tab
Vector<PGPPublicKeyRing> keyRings = Vector<PGPPublicKeyRing> keyRings =
(Vector<PGPPublicKeyRing>) Apg.getPublicKeyRings().clone(); (Vector<PGPPublicKeyRing>) Apg.getPublicKeyRings().clone();
Collections.sort(keyRings, new Apg.PublicKeySorter()); Collections.sort(keyRings, new Apg.PublicKeySorter());
@ -130,6 +143,34 @@ public class EncryptFileActivity extends BaseActivity {
} }
}); });
// symmetric tab
mAlgorithm = (Spinner) findViewById(R.id.algorithm);
Choice choices[] = {
new Choice(PGPEncryptedData.AES_128, "AES 128"),
new Choice(PGPEncryptedData.AES_192, "AES 192"),
new Choice(PGPEncryptedData.AES_256, "AES 256"),
new Choice(PGPEncryptedData.BLOWFISH, "Blowfish"),
new Choice(PGPEncryptedData.TWOFISH, "Twofish"),
new Choice(PGPEncryptedData.CAST5, "CAST5"),
new Choice(PGPEncryptedData.DES, "DES"),
new Choice(PGPEncryptedData.TRIPLE_DES, "Triple DES"),
new Choice(PGPEncryptedData.IDEA, "IDEA"),
};
ArrayAdapter<Choice> adapter =
new ArrayAdapter<Choice>(this, android.R.layout.simple_spinner_item, choices);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mAlgorithm.setAdapter(adapter);
for (int i = 0; i < choices.length; ++i) {
if (choices[i].getId() == PGPEncryptedData.AES_256) {
mAlgorithm.setSelection(i);
break;
}
}
mPassPhrase = (EditText) findViewById(R.id.pass_phrase);
mPassPhraseAgain = (EditText) findViewById(R.id.pass_phrase_again);
mEncryptButton.setOnClickListener(new OnClickListener() { mEncryptButton.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -232,7 +273,20 @@ public class EncryptFileActivity extends BaseActivity {
return; return;
} }
} else { } else {
// symmetric encryption
String passPhrase = mPassPhrase.getText().toString();
String passPhraseAgain = mPassPhraseAgain.getText().toString();
if (!passPhrase.equals(passPhraseAgain)) {
Toast.makeText(this, "Pass phrases don't match.",
Toast.LENGTH_SHORT).show();
return;
}
if (passPhrase.length() == 0) {
Toast.makeText(this, "Enter a pass phrase.",
Toast.LENGTH_SHORT).show();
return;
}
} }
askForOutputFilename(); askForOutputFilename();
@ -274,14 +328,16 @@ public class EncryptFileActivity extends BaseActivity {
boolean encryptIt = mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0; boolean encryptIt = mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0;
if (encryptIt) { if (encryptIt) {
Apg.encrypt(in, out, true, mEncryptionKeyIds, getSecretKeyId(), Apg.encrypt(in, out, mAsciiArmour.isSelected(),
Apg.getPassPhrase(), this); mEncryptionKeyIds, getSecretKeyId(),
} else { Apg.getPassPhrase(), this,
Apg.signText(in, out, getSecretKeyId(), PGPEncryptedData.AES_256, null);
Apg.getPassPhrase(), HashAlgorithmTags.SHA256, this);
} }
} else { } else {
Apg.encrypt(in, out, mAsciiArmour.isSelected(),
null, 0, null, this,
((Choice) mAlgorithm.getSelectedItem()).getId(),
mPassPhrase.getText().toString());
} }
out.close(); out.close();

View File

@ -25,6 +25,7 @@ import java.security.SignatureException;
import java.util.Vector; import java.util.Vector;
import org.bouncycastle2.bcpg.HashAlgorithmTags; import org.bouncycastle2.bcpg.HashAlgorithmTags;
import org.bouncycastle2.openpgp.PGPEncryptedData;
import org.bouncycastle2.openpgp.PGPException; import org.bouncycastle2.openpgp.PGPException;
import org.bouncycastle2.openpgp.PGPPublicKey; import org.bouncycastle2.openpgp.PGPPublicKey;
import org.bouncycastle2.openpgp.PGPPublicKeyRing; import org.bouncycastle2.openpgp.PGPPublicKeyRing;
@ -200,7 +201,8 @@ public class EncryptMessageActivity extends BaseActivity {
if (encryptIt) { if (encryptIt) {
Apg.encrypt(in, out, true, mEncryptionKeyIds, getSecretKeyId(), Apg.encrypt(in, out, true, mEncryptionKeyIds, getSecretKeyId(),
Apg.getPassPhrase(), this); Apg.getPassPhrase(), this,
PGPEncryptedData.AES_256, null);
} else { } else {
Apg.signText(in, out, getSecretKeyId(), Apg.signText(in, out, getSecretKeyId(),
Apg.getPassPhrase(), HashAlgorithmTags.SHA256, this); Apg.getPassPhrase(), HashAlgorithmTags.SHA256, this);

View File

@ -26,7 +26,6 @@ import org.bouncycastle2.openpgp.PGPSecretKey;
import org.thialfihar.android.apg.Apg; import org.thialfihar.android.apg.Apg;
import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R; import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.R.id;
import org.thialfihar.android.apg.utils.Choice; import org.thialfihar.android.apg.utils.Choice;
import android.app.DatePickerDialog; import android.app.DatePickerDialog;