Changeset 1260 for trunk


Ignore:
Timestamp:
03/19/19 10:39:24 (5 years ago)
Author:
bln4
Message:

Actions for Davids presentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/auth/sharing/Actions.java

    r1259 r1260  
    11package org.expeditee.auth.sharing;
    22
     3import java.security.InvalidKeyException;
     4import java.security.NoSuchAlgorithmException;
    35import java.security.SecureRandom;
     6import java.util.Arrays;
    47import java.util.Base64;
     8import java.util.Collection;
    59import java.util.HashMap;
    610import java.util.List;
     
    812import java.util.Random;
    913
     14import javax.crypto.BadPaddingException;
     15import javax.crypto.Cipher;
     16import javax.crypto.IllegalBlockSizeException;
     17import javax.crypto.NoSuchPaddingException;
     18import javax.crypto.SecretKey;
     19import javax.crypto.spec.SecretKeySpec;
     20
    1021import org.expeditee.gui.DisplayController;
     22import org.expeditee.gui.Frame;
     23import org.expeditee.gui.FrameIO;
    1124import org.expeditee.items.Text;
     25import org.ngikm.cryptography.CryptographyConstants;
    1226
    1327import com.codahale.shamir.Scheme;
    1428
    15 public class Actions {
     29public class Actions implements CryptographyConstants {
    1630       
    1731        public static Text GenerateSecret(String label) {
     
    2135        public static Text SplitSecret(Text key) {
    2236                return AuthSplitSecret(key);
     37        }
     38       
     39        public static Text JoinSecret(Text key) {
     40                return AuthJoinSecret(key);
    2341        }
    2442
     
    88106                return key;
    89107        }
     108       
     109        public static void AuthEncrypt(Text labelWithKey) {
     110                // Obtain encryption key
     111                String keyEncoded = labelWithKey.getData().get(0);
     112                byte[] keyDecoded = Base64.getDecoder().decode(keyEncoded);
     113                SecretKey key = new SecretKeySpec(keyDecoded, SymmetricAlgorithm);
     114               
     115                // Perform encryption
     116                Frame toEncrypt = FrameIO.LoadFrame(labelWithKey.getLink());
     117                Collection<Text> textItems = toEncrypt.getTextItems();
     118                for (Text t: textItems) {
     119                        byte[] encrypted = EncryptSymmetric(t.getText().getBytes(), key);
     120                        t.setText(Base64.getEncoder().encodeToString(encrypted));
     121                }
     122               
     123                // Save changes
     124                FrameIO.SaveFrame(toEncrypt);
     125        }
     126       
     127        public static void AuthDecrypt(Text labelWithKey) {
     128                // Obtain encryption key
     129                String keyEncoded = labelWithKey.getData().get(0);
     130                byte[] keyDecoded = Base64.getDecoder().decode(keyEncoded);
     131                SecretKey key = new SecretKeySpec(keyDecoded, SymmetricAlgorithm);
     132               
     133                // Perform decryption
     134                Frame toEncrypt = FrameIO.LoadFrame(labelWithKey.getLink());
     135                Collection<Text> textItems = toEncrypt.getTextItems();
     136                for (Text t: textItems) {
     137                        byte[] decrypted = DecryptSymmetric(Base64.getDecoder().decode(t.getText().getBytes()), key);
     138                        t.setText(new String(decrypted));
     139                }
     140        }
     141       
     142        private static byte[] EncryptSymmetric(final byte[] toEncrypt, final SecretKey key) {
     143                // toEncrypt = Base64.getDecoder().decode(toEncrypt);
     144                try {
     145                        final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
     146                        cipher.init(Cipher.ENCRYPT_MODE, key);
     147                        //could use modulus
     148                        final int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
     149                        final byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
     150                        //System.err.println("(" + toEncryptSizeAdjusted.length + ")" + "Before Encryption Data: "
     151                        //              + Arrays.toString(toEncryptSizeAdjusted));
     152                        final byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
     153                        //System.err.println("(" + result.length + ")" + "Encrypted Data: " + Arrays.toString(result));
     154                        return result;
     155                } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
     156                                | IllegalBlockSizeException | BadPaddingException e) {
     157                        e.printStackTrace();
     158                        return null;
     159                }
     160        }
     161
     162        private static byte[] DecryptSymmetric(final byte[] toDecrypt, final SecretKey key) {
     163                try {
     164                        final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
     165                        cipher.init(Cipher.DECRYPT_MODE, key);
     166                        final byte[] decryptedBytes = cipher.doFinal(toDecrypt);
     167                        int indexOfZero = decryptedBytes.length - 1;
     168                        for (int i = decryptedBytes.length - 1; i >= 0; i--) {
     169                                if (decryptedBytes[i] != (byte) 0) {
     170                                        indexOfZero = i + 1;
     171                                        break;
     172                                }
     173                        }
     174                        return Arrays.copyOf(decryptedBytes, indexOfZero);
     175                } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
     176                                | IllegalBlockSizeException | BadPaddingException e) {
     177                        e.printStackTrace();
     178                        return null;
     179                }
     180        }
    90181}
Note: See TracChangeset for help on using the changeset viewer.