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 EncryptedProfileExpWriter extends ExpWriter implements CryptographyConstants {
|
---|
22 | private SecretKey personalKey;
|
---|
23 | private static final String nl = "\n";
|
---|
24 |
|
---|
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);
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | protected void preOutputFrame() {
|
---|
34 | try {
|
---|
35 | final String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + nl;
|
---|
36 | _writer.write(line);
|
---|
37 | _stringWriter.append(line);
|
---|
38 | } catch (final IOException e) {
|
---|
39 | e.printStackTrace();
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | protected void writeLine(String line) throws IOException {
|
---|
45 | // do not write empty lines
|
---|
46 | if (line == null) { return; }
|
---|
47 |
|
---|
48 | // prepare line to write out
|
---|
49 | final byte[] encrypted = EncryptSymmetric(line.getBytes(), personalKey);
|
---|
50 | final String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
|
---|
51 |
|
---|
52 | // output
|
---|
53 | _writer.write(toWrite);
|
---|
54 | _stringWriter.append(toWrite);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private static byte[] EncryptSymmetric(final byte[] toEncrypt, final SecretKey key) {
|
---|
58 | try {
|
---|
59 | final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
|
---|
60 | cipher.init(Cipher.ENCRYPT_MODE, key);
|
---|
61 | //could use modulus
|
---|
62 | final int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
|
---|
63 | final byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
|
---|
64 | final byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
|
---|
65 | return result;
|
---|
66 | } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
---|
67 | | IllegalBlockSizeException | BadPaddingException e) {
|
---|
68 | e.printStackTrace();
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|