Changeset 1400 for trunk


Ignore:
Timestamp:
05/31/19 11:39:55 (5 years ago)
Author:
bln4
Message:

Can now encrypt specific items on an encrypted frame.

Location:
trunk/src/org/expeditee/encryption/io
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/encryption/io/EncryptedExpReader.java

    r1399 r1400  
    1212import java.util.Arrays;
    1313import java.util.Base64;
     14import java.util.Collection;
    1415
    1516import javax.crypto.BadPaddingException;
     
    2324import org.expeditee.gui.Frame;
    2425import org.expeditee.io.ExpReader;
     26import org.expeditee.items.Item;
    2527import org.expeditee.items.Text;
    2628import org.expeditee.settings.identity.secrets.KeyList;
     
    7274        public Frame readFrame(final BufferedReader reader) throws IOException {
    7375                if (accessDenied) { return null; }
    74                 else return super.readFrame(reader);
     76                else {
     77                        Frame unencryptedFrame = super.readFrame(reader);
     78                        individualItemDecryption(unencryptedFrame);
     79                        return unencryptedFrame;
     80                }
     81        }
     82       
     83        private void individualItemDecryption(Frame frame) {
     84                // Find items with their own encryption label.
     85                Collection<Item> itemsWithEncryptionLabel = frame.getAllItems();
     86                itemsWithEncryptionLabel.removeIf(i -> i.getEncryptionLabel() == null || i.getEncryptionLabel().isEmpty());
     87               
     88                for (Item i: itemsWithEncryptionLabel) {
     89                        // Decrypt the Text Items
     90                        if (i instanceof Text) {
     91                                byte[] keyBytes = EncryptedExpWriter.resolveKeyFromLabel(i.getEncryptionLabel(), "");
     92                                if (keyBytes == null) { continue; }
     93                                SecretKeySpec key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     94                                byte[] decryptedBytes = DecryptSymmetric(Base64.getDecoder().decode(i.getText()), key);
     95                                i.setText(new String(decryptedBytes));
     96                        }
     97                }
    7598        }
    7699       
  • trunk/src/org/expeditee/encryption/io/EncryptedExpWriter.java

    r1399 r1400  
    2222import org.expeditee.gui.MessageBay;
    2323import org.expeditee.io.ExpWriter;
     24import org.expeditee.items.Item;
    2425import org.expeditee.items.Text;
    2526import org.expeditee.settings.UserSettings;
     
    6061       
    6162        @Override
     63        public void outputFrame(Frame frame) throws IOException {
     64                individualItemEncryption(frame);
     65                super.outputFrame(frame);
     66        }
     67       
     68        @Override
    6269        protected void writeLine(String line) throws IOException {
    6370                // do not write empty lines
     
    7178                _writer.write(toWrite);
    7279                _stringWriter.append(toWrite);
     80        }
     81       
     82        private void individualItemEncryption(Frame frame) {
     83                // Find items with their own encryption label.
     84                Collection<Item> itemsWithEncryptionLabel = frame.getAllItems();
     85                itemsWithEncryptionLabel.removeIf(i -> i.getEncryptionLabel() == null || i.getEncryptionLabel().isEmpty());
     86               
     87                for (Item i: itemsWithEncryptionLabel) {
     88                        // Encrypt the Text Items
     89                        if (i instanceof Text) {
     90                                byte[] keyBytes = resolveKeyFromLabel(i.getEncryptionLabel(), "(actually item) on " + i.getParent().getName());
     91                                if (keyBytes == null) { continue; }
     92                                SecretKeySpec key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     93                                byte[] encryptedBytes = EncryptSymmetric(i.getText().getBytes(), key);
     94                                i.setText(Base64.getEncoder().encodeToString(encryptedBytes));
     95                        }
     96                }
    7397        }
    7498       
Note: See TracChangeset for help on using the changeset viewer.