only allow asymmetric encryption or symmetric encryption of files, not both like before

This commit is contained in:
Thialfihar 2010-04-29 01:38:19 +00:00
parent a037815454
commit 5cf07b4a03
3 changed files with 29 additions and 8 deletions

View File

@ -166,6 +166,14 @@ public class Apg {
} }
} }
public static class NoAsymmetricEncryptionException extends Exception {
static final long serialVersionUID = 0xf812773343L;
public NoAsymmetricEncryptionException() {
super();
}
}
static { static {
mPublicKeyRings = new Vector<PGPPublicKeyRing>(); mPublicKeyRings = new Vector<PGPPublicKeyRing>();
mSecretKeyRings = new Vector<PGPSecretKeyRing>(); mSecretKeyRings = new Vector<PGPSecretKeyRing>();
@ -1366,7 +1374,7 @@ public class Apg {
} }
public static long getDecryptionKeyId(InputStream inStream) public static long getDecryptionKeyId(InputStream inStream)
throws GeneralException, IOException { throws GeneralException, NoAsymmetricEncryptionException, IOException {
InputStream in = PGPUtil.getDecoderStream(inStream); InputStream in = PGPUtil.getDecoderStream(inStream);
PGPObjectFactory pgpF = new PGPObjectFactory(in); PGPObjectFactory pgpF = new PGPObjectFactory(in);
PGPEncryptedDataList enc; PGPEncryptedDataList enc;
@ -1387,9 +1395,11 @@ public class Apg {
// find the secret key // find the secret key
PGPSecretKey secretKey = null; PGPSecretKey secretKey = null;
Iterator it = enc.getEncryptedDataObjects(); Iterator it = enc.getEncryptedDataObjects();
boolean gotAsymmetricEncryption = false;
while (it.hasNext()) { while (it.hasNext()) {
Object obj = it.next(); Object obj = it.next();
if (obj instanceof PGPPublicKeyEncryptedData) { if (obj instanceof PGPPublicKeyEncryptedData) {
gotAsymmetricEncryption = true;
PGPPublicKeyEncryptedData pbe = (PGPPublicKeyEncryptedData) obj; PGPPublicKeyEncryptedData pbe = (PGPPublicKeyEncryptedData) obj;
secretKey = findSecretKey(pbe.getKeyID()); secretKey = findSecretKey(pbe.getKeyID());
if (secretKey != null) { if (secretKey != null) {
@ -1398,6 +1408,10 @@ public class Apg {
} }
} }
if (!gotAsymmetricEncryption) {
throw new NoAsymmetricEncryptionException();
}
if (secretKey == null) { if (secretKey == null) {
return 0; return 0;
} }

View File

@ -129,18 +129,23 @@ public class DecryptFileActivity extends BaseActivity {
try { try {
InputStream in = new FileInputStream(mInputFilename); InputStream in = new FileInputStream(mInputFilename);
setSecretKeyId(Apg.getDecryptionKeyId(in)); try {
if (getSecretKeyId() == 0) { setSecretKeyId(Apg.getDecryptionKeyId(in));
if (getSecretKeyId() == 0) {
throw new Apg.GeneralException("no suitable keys found");
}
mAssumeSymmetricEncryption = false;
} catch (Apg.NoAsymmetricEncryptionException e) {
setSecretKeyId(0);
// reopen the file to check whether there's symmetric encryption data in there // reopen the file to check whether there's symmetric encryption data in there
in = new FileInputStream(mInputFilename); in = new FileInputStream(mInputFilename);
if (!Apg.hasSymmetricEncryption(in)) { if (!Apg.hasSymmetricEncryption(in)) {
throw new Apg.GeneralException("no suitable keys found"); throw new Apg.GeneralException("no known kind of encryption found");
} }
mAssumeSymmetricEncryption = true; mAssumeSymmetricEncryption = true;
} else { }
mAssumeSymmetricEncryption = false;
} showDialog(Id.dialog.pass_phrase);
showDialog(Id.dialog.pass_phrase);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
error = "file not found: " + e.getLocalizedMessage(); error = "file not found: " + e.getLocalizedMessage();
} catch (IOException e) { } catch (IOException e) {

View File

@ -155,6 +155,8 @@ public class DecryptMessageActivity extends BaseActivity {
error = e.getLocalizedMessage(); error = e.getLocalizedMessage();
} catch (Apg.GeneralException e) { } catch (Apg.GeneralException e) {
error = e.getLocalizedMessage(); error = e.getLocalizedMessage();
} catch (Apg.NoAsymmetricEncryptionException e) {
error = "no asymmetric encryption found";
} }
if (error != null) { if (error != null) {
Toast.makeText(this, "Error: " + error, Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Error: " + error, Toast.LENGTH_SHORT).show();