- Timestamp:
- 03/19/19 10:39:24 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/auth/sharing/Actions.java
r1259 r1260 1 1 package org.expeditee.auth.sharing; 2 2 3 import java.security.InvalidKeyException; 4 import java.security.NoSuchAlgorithmException; 3 5 import java.security.SecureRandom; 6 import java.util.Arrays; 4 7 import java.util.Base64; 8 import java.util.Collection; 5 9 import java.util.HashMap; 6 10 import java.util.List; … … 8 12 import java.util.Random; 9 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 10 21 import org.expeditee.gui.DisplayController; 22 import org.expeditee.gui.Frame; 23 import org.expeditee.gui.FrameIO; 11 24 import org.expeditee.items.Text; 25 import org.ngikm.cryptography.CryptographyConstants; 12 26 13 27 import com.codahale.shamir.Scheme; 14 28 15 public class Actions {29 public class Actions implements CryptographyConstants { 16 30 17 31 public static Text GenerateSecret(String label) { … … 21 35 public static Text SplitSecret(Text key) { 22 36 return AuthSplitSecret(key); 37 } 38 39 public static Text JoinSecret(Text key) { 40 return AuthJoinSecret(key); 23 41 } 24 42 … … 88 106 return key; 89 107 } 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 } 90 181 }
Note:
See TracChangeset
for help on using the changeset viewer.