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