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 |
|
---|
9 | import javax.crypto.BadPaddingException;
|
---|
10 | import javax.crypto.Cipher;
|
---|
11 | import javax.crypto.IllegalBlockSizeException;
|
---|
12 | import javax.crypto.NoSuchPaddingException;
|
---|
13 | import javax.crypto.SecretKey;
|
---|
14 | import javax.crypto.spec.SecretKeySpec;
|
---|
15 |
|
---|
16 | import org.expeditee.io.ExpWriter;
|
---|
17 | import org.expeditee.items.Text;
|
---|
18 | import org.expeditee.settings.auth.secrets.KeyList;
|
---|
19 | import org.ngikm.cryptography.CryptographyConstants;
|
---|
20 |
|
---|
21 | public class EncryptedExpWriter extends ExpWriter implements CryptographyConstants {
|
---|
22 | private SecretKey key;
|
---|
23 | private String label;
|
---|
24 | private static final String nl = "\n";
|
---|
25 |
|
---|
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 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | protected void preOutputFrame() {
|
---|
42 | try {
|
---|
43 | String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + label + nl;
|
---|
44 | _writer.write(line);
|
---|
45 | _stringWriter.append(line);
|
---|
46 | } catch (final IOException e) {
|
---|
47 | e.printStackTrace();
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | protected void writeLine(String line) throws IOException {
|
---|
53 | // do not write empty lines
|
---|
54 | if (line == null) { return; }
|
---|
55 |
|
---|
56 | // prepare line to write out
|
---|
57 | byte[] encrypted = EncryptSymmetric(line.getBytes(), key);
|
---|
58 | String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
|
---|
59 |
|
---|
60 | // output
|
---|
61 | _writer.write(toWrite);
|
---|
62 | _stringWriter.append(toWrite);
|
---|
63 | }
|
---|
64 |
|
---|
65 | private byte[] resolveKeyFromLabel(String label) {
|
---|
66 | return null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | private static byte[] EncryptSymmetric(byte[] toEncrypt, SecretKey key) {
|
---|
70 | try {
|
---|
71 | Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
|
---|
72 | cipher.init(Cipher.ENCRYPT_MODE, key);
|
---|
73 | //could use modulus
|
---|
74 | int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
|
---|
75 | byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
|
---|
76 | byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
|
---|
77 | return result;
|
---|
78 | } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
---|
79 | | IllegalBlockSizeException | BadPaddingException e) {
|
---|
80 | e.printStackTrace();
|
---|
81 | return null;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|