Ignore:
Timestamp:
01/30/19 12:49:25 (5 years ago)
Author:
bln4
Message:

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:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/auth/EncryptedProfileExpWriter.java

    r1201 r1202  
    1 package org.expeditee.auth.io;
     1package org.expeditee.auth;
    22
    33import java.io.IOException;
    44import java.security.InvalidKeyException;
    5 import java.security.KeyStoreException;
    65import java.security.NoSuchAlgorithmException;
    7 import java.security.UnrecoverableEntryException;
    8 import java.security.cert.CertificateException;
    96import java.util.Arrays;
    107import java.util.Base64;
     
    1714import javax.crypto.spec.SecretKeySpec;
    1815
    19 import org.expeditee.auth.Authenticator;
    2016import org.expeditee.io.ExpWriter;
    21 import org.expeditee.settings.UserSettings;
     17import org.expeditee.items.Text;
     18import org.expeditee.settings.auth.secrets.KeyList;
    2219import org.ngikm.cryptography.CryptographyConstants;
    2320
    24 public class EncryptedExpWriter extends ExpWriter implements CryptographyConstants {
    25         private SecretKey key;
     21public class EncryptedProfileExpWriter extends ExpWriter implements CryptographyConstants {
     22        private SecretKey personalKey;
    2623        private static final String nl = "\n";
    2724       
    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                
     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);         
    4230        }
    4331
     
    4533        protected void preOutputFrame() {
    4634                try {
    47                         final String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + " " + UserSettings.UserName.get() + nl;
     35                        final String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + nl;
    4836                        _writer.write(line);
    4937                        _stringWriter.append(line);
     
    5947               
    6048                // prepare line to write out
    61                 final byte[] encrypted = EncryptSymmetric(line.getBytes(), key);
     49                final byte[] encrypted = EncryptSymmetric(line.getBytes(), personalKey);
    6250                final String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
    6351               
     
    6755        }
    6856       
    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        
    7557        private static byte[] EncryptSymmetric(final byte[] toEncrypt, final SecretKey key) {
    76                 // toEncrypt = Base64.getDecoder().decode(toEncrypt);
    7758                try {
    7859                        final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
     
    8162                        final int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
    8263                        final byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
    83                         //System.err.println("(" + toEncryptSizeAdjusted.length + ")" + "Before Encryption Data: "
    84                         //              + Arrays.toString(toEncryptSizeAdjusted));
    8564                        final byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
    86                         //System.err.println("(" + result.length + ")" + "Encrypted Data: " + Arrays.toString(result));
    8765                        return result;
    8866                } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
Note: See TracChangeset for help on using the changeset viewer.