1 | package org.expeditee.auth.io;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.security.InvalidKeyException;
|
---|
5 | import java.security.KeyStoreException;
|
---|
6 | import java.security.NoSuchAlgorithmException;
|
---|
7 | import java.security.UnrecoverableEntryException;
|
---|
8 | import java.security.cert.CertificateException;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Base64;
|
---|
11 |
|
---|
12 | import javax.crypto.BadPaddingException;
|
---|
13 | import javax.crypto.Cipher;
|
---|
14 | import javax.crypto.IllegalBlockSizeException;
|
---|
15 | import javax.crypto.NoSuchPaddingException;
|
---|
16 | import javax.crypto.SecretKey;
|
---|
17 | import javax.crypto.spec.SecretKeySpec;
|
---|
18 |
|
---|
19 | import org.expeditee.auth.Authenticator;
|
---|
20 | import org.expeditee.io.ExpWriter;
|
---|
21 | import org.expeditee.settings.UserSettings;
|
---|
22 | import org.ngikm.cryptography.CryptographyConstants;
|
---|
23 |
|
---|
24 | public class EncryptedExpWriter extends ExpWriter implements CryptographyConstants {
|
---|
25 | private SecretKey key;
|
---|
26 | private static final String nl = "\n";
|
---|
27 |
|
---|
28 | public EncryptedExpWriter() throws IOException {
|
---|
29 | try {
|
---|
30 | final Authenticator auth = new Authenticator();
|
---|
31 | SecretKey key = auth.getSecretKey(UserSettings.UserName.get(), System.getProperty("password"));
|
---|
32 | if (key == null) {
|
---|
33 | final byte[] keyBytes = pad(UserSettings.UserName.get().getBytes("UTF-8"));
|
---|
34 | key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
35 | auth.putKey(UserSettings.UserName.get(), System.getProperty("password"), key);
|
---|
36 | }
|
---|
37 | this.key = key;
|
---|
38 | } catch (final KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | UnrecoverableEntryException e) {
|
---|
39 | e.printStackTrace();
|
---|
40 | }
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | protected void preOutputFrame() {
|
---|
46 | try {
|
---|
47 | final String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + " " + UserSettings.UserName.get() + 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 | final byte[] encrypted = EncryptSymmetric(line.getBytes(), key);
|
---|
62 | final String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
|
---|
63 |
|
---|
64 | // output
|
---|
65 | _writer.write(toWrite);
|
---|
66 | _stringWriter.append(toWrite);
|
---|
67 | }
|
---|
68 |
|
---|
69 | private byte[] pad(final byte[] bytes) {
|
---|
70 | int c = 16;
|
---|
71 | while (c - bytes.length < 0) { c *= 2; }
|
---|
72 | return Arrays.copyOf(bytes, c);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private static byte[] EncryptSymmetric(final byte[] toEncrypt, final SecretKey key) {
|
---|
76 | // toEncrypt = Base64.getDecoder().decode(toEncrypt);
|
---|
77 | try {
|
---|
78 | final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
|
---|
79 | cipher.init(Cipher.ENCRYPT_MODE, key);
|
---|
80 | //could use modulus
|
---|
81 | final int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
|
---|
82 | final byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
|
---|
83 | //System.err.println("(" + toEncryptSizeAdjusted.length + ")" + "Before Encryption Data: "
|
---|
84 | // + Arrays.toString(toEncryptSizeAdjusted));
|
---|
85 | final byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
|
---|
86 | //System.err.println("(" + result.length + ")" + "Encrypted Data: " + Arrays.toString(result));
|
---|
87 | return result;
|
---|
88 | } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
---|
89 | | IllegalBlockSizeException | BadPaddingException e) {
|
---|
90 | e.printStackTrace();
|
---|
91 | return null;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|