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

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

org.expeditee.auth.EncryptedExpReader ->
org.expeditee.auth.EncryptedExpWriter ->
Actions ->
AttributeUtils ->
Frame ->
FrameIO ->
UserSettings ->

Changed how reading and writing encrypted files worked. A Frame attribute is now consulted to determine what to use as key for encryption. The 'profile' attribute setting is used to signal that the users personal aes key is used. Further enhancement will mean that other labels will be able to be used.


Actions ->

MailMode action now consults the database to reaquire the mail.

File size: 2.6 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 EncryptedExpWriter extends ExpWriter implements CryptographyConstants {
22 private SecretKey key;
23 private String label;
24 private static final String nl = "\n";
25
26 public EncryptedExpWriter(String encryptionLabel) throws IOException {
27 if (encryptionLabel.compareTo("Profile") == 0) {
28 // obtain personal key
29 Text text = KeyList.PersonalKey.get();
30 byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0));
31 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
32 label = "Profile";
33 } else {
34 byte[] keyBytes = resolveKeyFromLabel(encryptionLabel);
35 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
36 label = encryptionLabel;
37 }
38 }
39
40 @Override
41 protected void preOutputFrame() {
42 try {
43 String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + label + nl;
44 _writer.write(line);
45 _stringWriter.append(line);
46 } catch (final IOException e) {
47 e.printStackTrace();
48 }
49 }
50
51 @Override
52 protected void writeLine(String line) throws IOException {
53 // do not write empty lines
54 if (line == null) { return; }
55
56 // prepare line to write out
57 byte[] encrypted = EncryptSymmetric(line.getBytes(), key);
58 String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl;
59
60 // output
61 _writer.write(toWrite);
62 _stringWriter.append(toWrite);
63 }
64
65 private byte[] resolveKeyFromLabel(String label) {
66 return null;
67 }
68
69 private static byte[] EncryptSymmetric(byte[] toEncrypt, SecretKey key) {
70 try {
71 Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters);
72 cipher.init(Cipher.ENCRYPT_MODE, key);
73 //could use modulus
74 int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16);
75 byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length);
76 byte[] result = cipher.doFinal(toEncryptSizeAdjusted);
77 return result;
78 } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
79 | IllegalBlockSizeException | BadPaddingException e) {
80 e.printStackTrace();
81 return null;
82 }
83 }
84}
Note: See TracBrowser for help on using the repository browser.