1 | package org.expeditee.auth;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.security.InvalidKeyException;
|
---|
5 | import java.security.NoSuchAlgorithmException;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Base64;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.crypto.BadPaddingException;
|
---|
11 | import javax.crypto.Cipher;
|
---|
12 | import javax.crypto.IllegalBlockSizeException;
|
---|
13 | import javax.crypto.NoSuchPaddingException;
|
---|
14 | import javax.crypto.SecretKey;
|
---|
15 | import javax.crypto.spec.SecretKeySpec;
|
---|
16 |
|
---|
17 | import org.expeditee.io.ExpWriter;
|
---|
18 | import org.expeditee.items.Text;
|
---|
19 | import org.expeditee.settings.identity.secrets.KeyList;
|
---|
20 | import org.ngikm.cryptography.CryptographyConstants;
|
---|
21 |
|
---|
22 | public 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 | }
|
---|