source: trunk/src/org/expeditee/auth/EncryptedProfileExpWriter.java@ 1202

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

Initial commit of functionality concerning multiuser login, further to come.

Actions.java -> Actions that allow users to authenticate and secure their accounts.
AuthenticationTag.java -> Enum like structure for text fields associated with authentication.
Authenticator.java -> Startup functionality for when Expeditee is run in authentication mode.
EncryptedExpReader.java -> Reads exp files previously encrypted with EncryptedExpWriter (not currently used) and EncryptedProfileExpWriter
Mail.java -> Functions for transforming database stored messages into datastructures used to display those messages to the MailBay.

File size: 2.4 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;
8
9import javax.crypto.BadPaddingException;
10import javax.crypto.Cipher;
11import javax.crypto.IllegalBlockSizeException;
12import javax.crypto.NoSuchPaddingException;
13import javax.crypto.SecretKey;
14import javax.crypto.spec.SecretKeySpec;
15
16import org.expeditee.io.ExpWriter;
17import org.expeditee.items.Text;
18import org.expeditee.settings.auth.secrets.KeyList;
19import org.ngikm.cryptography.CryptographyConstants;
20
21public 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}
Note: See TracBrowser for help on using the repository browser.