source: trunk/src/org/expeditee/auth/io/EncryptedExpWriter.java@ 1200

Last change on this file since 1200 was 1200, checked in by bln4, 6 years ago

org.expeditee.auth.gio.EncryptedExpReader ->
org.expeditee.auth.gio.EncryptedExpWriter ->
org.expeditee.io.DefaultFrameReader ->
org.expeditee.io.DefaultFrameWriter ->
org.expeditee.io.ExpReader ->
org.expeditee.io.ExpWriter ->

The beginnings of a authentication system for Expeditee. Frame files (.exp) can now be encrypted with a password. A login system is being developed to accompany this.


org.expeditee.gui.AttributeUtils ->
org.expeditee.gui.Frame ->
org.expeditee.gui.FrameUtils ->
org.expeditee.items.Item ->
org.expeditee.items.PermissionPair ->
org.expeditee.items.Text ->

As part of the development the login screen. Modifications to Text Items have been made. New properties are 'Mask' and 'MinWidth'. Mask allows you to set a character to use as...a mask...for example, a password field using '*'. MinWidth allows you to specify a minimum width for text boxes. A text box with a minimum width never shrinks below this width; even when it has no content.

File size: 3.3 KB
Line 
1package org.expeditee.auth.io;
2
3import java.io.IOException;
4import java.security.InvalidKeyException;
5import java.security.KeyStoreException;
6import java.security.NoSuchAlgorithmException;
7import java.security.UnrecoverableEntryException;
8import java.security.cert.CertificateException;
9import java.util.Arrays;
10import java.util.Base64;
11
12import javax.crypto.BadPaddingException;
13import javax.crypto.Cipher;
14import javax.crypto.IllegalBlockSizeException;
15import javax.crypto.NoSuchPaddingException;
16import javax.crypto.SecretKey;
17import javax.crypto.spec.SecretKeySpec;
18
19import org.expeditee.auth.Authenticator;
20import org.expeditee.io.ExpWriter;
21import org.expeditee.settings.UserSettings;
22import org.ngikm.cryptography.CryptographyConstants;
23
24public 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}
Note: See TracBrowser for help on using the repository browser.