Changeset 1227 for trunk/src/org/expeditee/auth/EncryptedExpWriter.java
- Timestamp:
- 01/31/19 16:34:06 (5 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/auth/EncryptedExpWriter.java
r1226 r1227 19 19 import org.ngikm.cryptography.CryptographyConstants; 20 20 21 public class EncryptedProfileExpWriter extends ExpWriter implements CryptographyConstants { 22 private SecretKey personalKey; 21 public class EncryptedExpWriter extends ExpWriter implements CryptographyConstants { 22 private SecretKey key; 23 private String label; 23 24 private static final String nl = "\n"; 24 25 25 public EncryptedProfileExpWriter() throws IOException { 26 // obtain personal key 27 final Text text = KeyList.PersonalKey.get(); 28 final byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0)); 29 personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 26 public EncryptedExpWriter(String encryptionLabel) throws IOException { 27 if (encryptionLabel.compareTo("Profile") == 0) { 28 // obtain personal key 29 Text text = KeyList.PersonalKey.get(); 30 byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0)); 31 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 32 label = "Profile"; 33 } else { 34 byte[] keyBytes = resolveKeyFromLabel(encryptionLabel); 35 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 36 label = encryptionLabel; 37 } 30 38 } 31 39 … … 33 41 protected void preOutputFrame() { 34 42 try { 35 final String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG+ nl;43 String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + label + nl; 36 44 _writer.write(line); 37 45 _stringWriter.append(line); … … 47 55 48 56 // prepare line to write out 49 final byte[] encrypted = EncryptSymmetric(line.getBytes(), personalKey);50 finalString toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;57 byte[] encrypted = EncryptSymmetric(line.getBytes(), key); 58 String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl; 51 59 52 60 // output … … 55 63 } 56 64 57 private static byte[] EncryptSymmetric(final byte[] toEncrypt, final SecretKey key) { 65 private byte[] resolveKeyFromLabel(String label) { 66 return null; 67 } 68 69 private static byte[] EncryptSymmetric(byte[] toEncrypt, SecretKey key) { 58 70 try { 59 finalCipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);71 Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters); 60 72 cipher.init(Cipher.ENCRYPT_MODE, key); 61 73 //could use modulus 62 finalint length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);63 finalbyte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);64 finalbyte[] result = cipher.doFinal(toEncryptSizeAdjusted);74 int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16); 75 byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length); 76 byte[] result = cipher.doFinal(toEncryptSizeAdjusted); 65 77 return result; 66 78 } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
Note:
See TracChangeset
for help on using the changeset viewer.