[1363] | 1 | package org.expeditee.encryption;
|
---|
[1259] | 2 |
|
---|
[1260] | 3 | import java.security.InvalidKeyException;
|
---|
| 4 | import java.security.NoSuchAlgorithmException;
|
---|
[1259] | 5 | import java.security.SecureRandom;
|
---|
[1260] | 6 | import java.util.Arrays;
|
---|
[1259] | 7 | import java.util.Base64;
|
---|
[1260] | 8 | import java.util.Collection;
|
---|
[1259] | 9 | import java.util.HashMap;
|
---|
| 10 | import java.util.List;
|
---|
| 11 | import java.util.Map;
|
---|
| 12 | import java.util.Random;
|
---|
| 13 |
|
---|
[1260] | 14 | import javax.crypto.BadPaddingException;
|
---|
| 15 | import javax.crypto.Cipher;
|
---|
| 16 | import javax.crypto.IllegalBlockSizeException;
|
---|
| 17 | import javax.crypto.NoSuchPaddingException;
|
---|
| 18 | import javax.crypto.SecretKey;
|
---|
| 19 | import javax.crypto.spec.SecretKeySpec;
|
---|
| 20 |
|
---|
[1363] | 21 | import org.expeditee.auth.AuthenticatorBrowser;
|
---|
[1259] | 22 | import org.expeditee.gui.DisplayController;
|
---|
[1260] | 23 | import org.expeditee.gui.Frame;
|
---|
| 24 | import org.expeditee.gui.FrameIO;
|
---|
[1408] | 25 | import org.expeditee.items.Item;
|
---|
[1259] | 26 | import org.expeditee.items.Text;
|
---|
[1363] | 27 | import org.expeditee.settings.UserSettings;
|
---|
[1259] | 28 |
|
---|
| 29 | import com.codahale.shamir.Scheme;
|
---|
| 30 |
|
---|
[1260] | 31 | public class Actions implements CryptographyConstants {
|
---|
[1259] | 32 |
|
---|
[1414] | 33 | public static void TestSurrogate(Item primary) {
|
---|
[1408] | 34 | System.out.println("Test surrogates: ");
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[1259] | 37 | public static Text GenerateSecret(String label) {
|
---|
| 38 | return AuthGenerateSecret(label);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public static Text SplitSecret(Text key) {
|
---|
| 42 | return AuthSplitSecret(key);
|
---|
| 43 | }
|
---|
[1260] | 44 |
|
---|
| 45 | public static Text JoinSecret(Text key) {
|
---|
| 46 | return AuthJoinSecret(key);
|
---|
| 47 | }
|
---|
[1261] | 48 |
|
---|
| 49 | public static void Encrypt(Text labelWithKey) {
|
---|
| 50 | AuthEncrypt(labelWithKey);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public static void Decrypt(Text labelWithKey) {
|
---|
| 54 | AuthDecrypt(labelWithKey);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[1259] | 57 | public static Text AuthGenerateSecret(String label) {
|
---|
| 58 | // Generate AES Key
|
---|
| 59 | Random rand = new SecureRandom();
|
---|
| 60 | byte[] keyBytes = new byte[16];
|
---|
| 61 | rand.nextBytes(keyBytes);
|
---|
| 62 | String key = Base64.getEncoder().encodeToString(keyBytes);
|
---|
| 63 |
|
---|
| 64 | Text text = new Text(label);
|
---|
| 65 | text.setData(key);
|
---|
| 66 | text.setPosition(DisplayController.getMousePosition());
|
---|
| 67 | return text;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public static Text AuthSplitSecret(Text key) {
|
---|
| 71 | // Retrieve secret
|
---|
| 72 | String secret = key.getData().get(0);
|
---|
| 73 | byte[] secretBytes = Base64.getDecoder().decode(secret);
|
---|
| 74 |
|
---|
| 75 | // Obtain parameters for shamir
|
---|
| 76 | String[] arguments = key.getText().split(" ");
|
---|
[1310] | 77 | int requiredShares = Integer.parseInt(arguments[1]);
|
---|
| 78 | int totalShares = arguments.length - 2;
|
---|
[1259] | 79 |
|
---|
| 80 | // Create shares
|
---|
| 81 | Scheme scheme = new Scheme(new SecureRandom(), totalShares, requiredShares);
|
---|
| 82 | Map<Integer, byte[]> split = scheme.split(secretBytes);
|
---|
| 83 |
|
---|
| 84 | // Add shares to key
|
---|
| 85 | List<String> data = key.getData();
|
---|
| 86 | for (Integer i: split.keySet()) {
|
---|
| 87 | data.add("{" + i + "}" + Base64.getEncoder().encodeToString(split.get(i)));
|
---|
| 88 | }
|
---|
| 89 | key.setData(data);
|
---|
| 90 |
|
---|
| 91 | return key;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public static Text AuthJoinSecret(Text key) {
|
---|
| 95 | Map<Integer, byte[]> contributingParts = new HashMap<Integer, byte[]>();
|
---|
| 96 |
|
---|
| 97 | // Retrieve splits
|
---|
| 98 | List<String> data = key.getData();
|
---|
| 99 | for (String d: data) {
|
---|
| 100 | if (d.startsWith("{")) {
|
---|
| 101 | String[] split = d.split("}");
|
---|
| 102 | Integer k = Integer.parseInt(split[0].replace("{", ""));
|
---|
| 103 | byte[] v = Base64.getDecoder().decode(split[1]);
|
---|
| 104 | contributingParts.put(k, v);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | // Obtain parameters for shamir
|
---|
| 109 | String[] arguments = key.getText().split(" ");
|
---|
[1310] | 110 | int totalShares = Integer.parseInt(arguments[2]);
|
---|
| 111 | int requiredShares = Integer.parseInt(arguments[1]);
|
---|
[1259] | 112 |
|
---|
| 113 | // Perform join
|
---|
| 114 | Scheme scheme = new Scheme(new SecureRandom(), totalShares, requiredShares);
|
---|
| 115 | byte[] join = scheme.join(contributingParts);
|
---|
| 116 |
|
---|
| 117 | data.clear();
|
---|
| 118 | data.add(Base64.getEncoder().encodeToString(join));
|
---|
| 119 |
|
---|
| 120 | return key;
|
---|
| 121 | }
|
---|
[1260] | 122 |
|
---|
| 123 | public static void AuthEncrypt(Text labelWithKey) {
|
---|
| 124 | // Obtain encryption key
|
---|
| 125 | String keyEncoded = labelWithKey.getData().get(0);
|
---|
| 126 | byte[] keyDecoded = Base64.getDecoder().decode(keyEncoded);
|
---|
| 127 | SecretKey key = new SecretKeySpec(keyDecoded, SymmetricAlgorithm);
|
---|
| 128 |
|
---|
| 129 | // Perform encryption
|
---|
| 130 | Frame toEncrypt = FrameIO.LoadFrame(labelWithKey.getLink());
|
---|
| 131 | Collection<Text> textItems = toEncrypt.getTextItems();
|
---|
| 132 | for (Text t: textItems) {
|
---|
| 133 | byte[] encrypted = EncryptSymmetric(t.getText().getBytes(), key);
|
---|
| 134 | t.setText(Base64.getEncoder().encodeToString(encrypted));
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | // Save changes
|
---|
| 138 | FrameIO.SaveFrame(toEncrypt);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | public static void AuthDecrypt(Text labelWithKey) {
|
---|
| 142 | // Obtain encryption key
|
---|
| 143 | String keyEncoded = labelWithKey.getData().get(0);
|
---|
| 144 | byte[] keyDecoded = Base64.getDecoder().decode(keyEncoded);
|
---|
| 145 | SecretKey key = new SecretKeySpec(keyDecoded, SymmetricAlgorithm);
|
---|
| 146 |
|
---|
| 147 | // Perform decryption
|
---|
[1261] | 148 | Frame toDecrypt = FrameIO.LoadFrame(labelWithKey.getLink());
|
---|
| 149 | Collection<Text> textItems = toDecrypt.getTextItems();
|
---|
[1260] | 150 | for (Text t: textItems) {
|
---|
| 151 | byte[] decrypted = DecryptSymmetric(Base64.getDecoder().decode(t.getText().getBytes()), key);
|
---|
| 152 | t.setText(new String(decrypted));
|
---|
| 153 | }
|
---|
[1261] | 154 |
|
---|
| 155 | // Save changes
|
---|
| 156 | FrameIO.SaveFrame(toDecrypt);
|
---|
[1260] | 157 | }
|
---|
| 158 |
|
---|
[1361] | 159 | public static byte[] EncryptSymmetric(final byte[] toEncrypt, final SecretKey key) {
|
---|
[1260] | 160 | // toEncrypt = Base64.getDecoder().decode(toEncrypt);
|
---|
| 161 | try {
|
---|
| 162 | final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
|
---|
| 163 | cipher.init(Cipher.ENCRYPT_MODE, key);
|
---|
| 164 | //could use modulus
|
---|
| 165 | final int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
|
---|
| 166 | final byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
|
---|
| 167 | //System.err.println("(" + toEncryptSizeAdjusted.length + ")" + "Before Encryption Data: "
|
---|
| 168 | // + Arrays.toString(toEncryptSizeAdjusted));
|
---|
| 169 | final byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
|
---|
| 170 | //System.err.println("(" + result.length + ")" + "Encrypted Data: " + Arrays.toString(result));
|
---|
| 171 | return result;
|
---|
| 172 | } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
---|
| 173 | | IllegalBlockSizeException | BadPaddingException e) {
|
---|
| 174 | e.printStackTrace();
|
---|
| 175 | return null;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[1361] | 179 | public static byte[] DecryptSymmetric(final byte[] toDecrypt, final SecretKey key) {
|
---|
[1260] | 180 | try {
|
---|
| 181 | final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
|
---|
| 182 | cipher.init(Cipher.DECRYPT_MODE, key);
|
---|
| 183 | final byte[] decryptedBytes = cipher.doFinal(toDecrypt);
|
---|
| 184 | int indexOfZero = decryptedBytes.length - 1;
|
---|
| 185 | for (int i = decryptedBytes.length - 1; i >= 0; i--) {
|
---|
| 186 | if (decryptedBytes[i] != (byte) 0) {
|
---|
| 187 | indexOfZero = i + 1;
|
---|
| 188 | break;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | return Arrays.copyOf(decryptedBytes, indexOfZero);
|
---|
| 192 | } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
---|
| 193 | | IllegalBlockSizeException | BadPaddingException e) {
|
---|
| 194 | e.printStackTrace();
|
---|
| 195 | return null;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
[1363] | 198 |
|
---|
| 199 | public static void AuthAddSecretKey(String name, String data) {
|
---|
| 200 | Frame secretsFrame = FrameIO.LoadFrame(UserSettings.UserName.get() + AuthenticatorBrowser.SECRETS_FRAME);
|
---|
| 201 | secretsFrame.addText(500, 500, name, null).addToData(data);
|
---|
| 202 | }
|
---|
[1259] | 203 | }
|
---|