source: trunk/src/org/expeditee/auth/EncryptedExpWriter.java@ 1306

Last change on this file since 1306 was 1243, checked in by bln4, 5 years ago

Alterations to how mail works to work better in a cloud environment.
General code tidying and updating to work with larger changes happening to Expeditee.

File size: 2.8 KB
Line 
1package org.expeditee.auth;
2
3import java.io.IOException;
4import java.security.InvalidKeyException;
5import java.security.NoSuchAlgorithmException;
6import java.util.Arrays;
7import java.util.Base64;
8import java.util.List;
9
10import javax.crypto.BadPaddingException;
11import javax.crypto.Cipher;
12import javax.crypto.IllegalBlockSizeException;
13import javax.crypto.NoSuchPaddingException;
14import javax.crypto.SecretKey;
15import javax.crypto.spec.SecretKeySpec;
16
17import org.expeditee.io.ExpWriter;
18import org.expeditee.items.Text;
19import org.expeditee.settings.identity.secrets.KeyList;
20import org.ngikm.cryptography.CryptographyConstants;
21
22public class EncryptedExpWriter extends ExpWriter implements CryptographyConstants {
23 private SecretKey key;
24 private String label;
25 private static final String nl = "\n";
26
27 public EncryptedExpWriter(String encryptionLabel) throws IOException {
28 if (encryptionLabel.compareTo("Profile") == 0) {
29 // obtain personal key
30 Text text = KeyList.PersonalKey.get();
31 List<String> data = text.getData();
32 if (data != null && !data.isEmpty()) {
33 byte[] keyBytes = Base64.getDecoder().decode(data.get(0));
34 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
35 }
36 label = "Profile";
37 } else {
38 byte[] keyBytes = resolveKeyFromLabel(encryptionLabel);
39 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
40 label = encryptionLabel;
41 }
42 }
43
44 @Override
45 protected void preOutputFrame() {
46 try {
47 String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + label + nl;
48 _writer.write(line);
49 _stringWriter.append(line);
50 } catch (final IOException e) {
51 e.printStackTrace();
52 }
53 }
54
55 @Override
56 protected void writeLine(String line) throws IOException {
57 // do not write empty lines
58 if (line == null) { return; }
59
60 // prepare line to write out
61 byte[] encrypted = EncryptSymmetric(line.getBytes(), key);
62 String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
63
64 // output
65 _writer.write(toWrite);
66 _stringWriter.append(toWrite);
67 }
68
69 private byte[] resolveKeyFromLabel(String label) {
70 return null;
71 }
72
73 private static byte[] EncryptSymmetric(byte[] toEncrypt, SecretKey key) {
74 try {
75 Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
76 cipher.init(Cipher.ENCRYPT_MODE, key);
77 //could use modulus
78 int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
79 byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
80 byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
81 return result;
82 } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
83 | IllegalBlockSizeException | BadPaddingException e) {
84 e.printStackTrace();
85 return null;
86 }
87 }
88}
Note: See TracBrowser for help on using the repository browser.