- Timestamp:
- 01/30/19 12:49:25 (5 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/auth/EncryptedProfileExpWriter.java
r1201 r1202 1 package org.expeditee.auth .io;1 package org.expeditee.auth; 2 2 3 3 import java.io.IOException; 4 4 import java.security.InvalidKeyException; 5 import java.security.KeyStoreException;6 5 import java.security.NoSuchAlgorithmException; 7 import java.security.UnrecoverableEntryException;8 import java.security.cert.CertificateException;9 6 import java.util.Arrays; 10 7 import java.util.Base64; … … 17 14 import javax.crypto.spec.SecretKeySpec; 18 15 19 import org.expeditee.auth.Authenticator;20 16 import org.expeditee.io.ExpWriter; 21 import org.expeditee.settings.UserSettings; 17 import org.expeditee.items.Text; 18 import org.expeditee.settings.auth.secrets.KeyList; 22 19 import org.ngikm.cryptography.CryptographyConstants; 23 20 24 public class Encrypted ExpWriter extends ExpWriter implements CryptographyConstants {25 private SecretKey key;21 public class EncryptedProfileExpWriter extends ExpWriter implements CryptographyConstants { 22 private SecretKey personalKey; 26 23 private static final String nl = "\n"; 27 24 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); 42 30 } 43 31 … … 45 33 protected void preOutputFrame() { 46 34 try { 47 final String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + " " + UserSettings.UserName.get() +nl;35 final String line = EncryptedExpReader.ENCRYPTED_EXP_FLAG + nl; 48 36 _writer.write(line); 49 37 _stringWriter.append(line); … … 59 47 60 48 // prepare line to write out 61 final byte[] encrypted = EncryptSymmetric(line.getBytes(), key);49 final byte[] encrypted = EncryptSymmetric(line.getBytes(), personalKey); 62 50 final String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl; 63 51 … … 67 55 } 68 56 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 57 private static byte[] EncryptSymmetric(final byte[] toEncrypt, final SecretKey key) { 76 // toEncrypt = Base64.getDecoder().decode(toEncrypt);77 58 try { 78 59 final Cipher cipher = Cipher.getInstance(SymmetricAlgorithm + SymmetricAlgorithmParameters); … … 81 62 final int length = (int) ((Math.ceil(toEncrypt.length / 16f)) * 16); 82 63 final byte[] toEncryptSizeAdjusted = Arrays.copyOf(toEncrypt, length); 83 //System.err.println("(" + toEncryptSizeAdjusted.length + ")" + "Before Encryption Data: "84 // + Arrays.toString(toEncryptSizeAdjusted));85 64 final byte[] result = cipher.doFinal(toEncryptSizeAdjusted); 86 //System.err.println("(" + result.length + ")" + "Encrypted Data: " + Arrays.toString(result));87 65 return result; 88 66 } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
Note:
See TracChangeset
for help on using the changeset viewer.