Changeset 1277 for trunk/src/org/expeditee/auth/Mail.java
- Timestamp:
- 04/02/19 15:22:06 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/auth/Mail.java
r1243 r1277 1 1 package org.expeditee.auth; 2 2 3 import java.io.File; 4 import java.io.IOException; 3 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 4 7 import java.security.InvalidKeyException; 8 import java.security.KeyStoreException; 5 9 import java.security.NoSuchAlgorithmException; 6 10 import java.security.PrivateKey; 7 11 import java.security.PublicKey; 12 import java.security.cert.CertificateException; 13 import java.security.spec.InvalidKeySpecException; 8 14 import java.sql.Connection; 9 15 import java.sql.DriverManager; 10 16 import java.sql.PreparedStatement; 11 17 import java.sql.SQLException; 18 import java.sql.Statement; 12 19 import java.util.ArrayList; 13 20 import java.util.Arrays; … … 22 29 import javax.crypto.NoSuchPaddingException; 23 30 31 import org.expeditee.gui.FrameIO; 24 32 import org.ngikm.cryptography.CryptographyConstants; 25 33 … … 39 47 } 40 48 41 public static void sendMail(MailEntry mail, PublicKey key, Path outbox) { 49 public static void sendMail(MailEntry mail, String colleagueName) { 50 // Ensure dead drop area is set up. 51 Path databaseFileDirPath = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(colleagueName); 52 Path databaseFilePath = databaseFileDirPath.resolve(colleagueName + ".db"); 53 File databaseFile = databaseFilePath.toFile(); 54 if (!databaseFile.exists()) { 55 databaseFileDirPath.toFile().mkdirs(); 56 String sql = 57 "CREATE TABLE EXPMAIL (" + 58 "TIME TEXT NOT NULL, " + 59 "SND TEXT NOT NULL, " + 60 "REC TEXT NOT NULL, " + 61 "MSG TEXT NOT NULL, " + 62 "MSG2 TEXT NOT NULL, " + 63 "OPTS ARRAY NOT NULL, " + 64 "OPTSVAL ARRAY NOT NULL)"; 65 try { 66 Connection c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath()); 67 Statement createTable = c.createStatement(); 68 createTable.executeUpdate(sql); 69 createTable.close(); 70 c.close(); 71 } catch (SQLException e) { 72 System.err.println("Error while creating database file."); 73 e.printStackTrace(); 74 } 75 } 76 77 // Obtain public key 78 PublicKey publicKey = null; 79 try { 80 publicKey = Authenticator.getInstance().getPublicKey(colleagueName); 81 } catch (InvalidKeySpecException | NoSuchAlgorithmException | KeyStoreException | CertificateException 82 | ClassNotFoundException | IOException | SQLException e) { 83 System.err.println("Error while sending message. Unable to obtain public key for colleague " + 84 colleagueName + ". Exception message: " + e.getMessage()); 85 return; 86 } 87 88 // Check we got public key 89 if (publicKey == null) { 90 System.err.println("Error while sending message. Unable to obtain public key for colleague. Have you exchanged contact details?"); 91 return; 92 } 93 94 // Send message 95 sendMail(mail, publicKey, databaseFilePath); 96 } 97 98 private static void sendMail(MailEntry mail, PublicKey key, Path databaseFile) { 42 99 try { 43 100 Cipher cipher = Cipher.getInstance(AsymmetricAlgorithm + AsymmetricAlgorithmParameters); 44 101 45 102 // encrypt the necessary parts of the message 46 cipher.init(Cipher.ENCRYPT_MODE, key);47 String time = Base64.getEncoder().encodeToString(cipher.doFinal(mail.timestamp.getBytes()));103 //cipher.init(Cipher.ENCRYPT_MODE, key); 104 //String time = Base64.getEncoder().encodeToString(cipher.doFinal(mail.timestamp.getBytes())); 48 105 cipher.init(Cipher.ENCRYPT_MODE, key); 49 106 String sender = Base64.getEncoder().encodeToString(cipher.doFinal(mail.sender.getBytes())); … … 65 122 66 123 // write to mail database 67 Connection c = DriverManager.getConnection("jdbc:sqlite:" + outbox.resolve("expmail.db"));124 Connection c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile); 68 125 String sql = "INSERT INTO EXPMAIL (TIME,SND,REC,MSG,MSG2,OPTS,OPTSVAL) VALUES (?, ?, ?, ?, ?, ?, ?);"; 69 126 PreparedStatement statement = c.prepareStatement(sql); 70 statement.setString(1, time);127 statement.setString(1, mail.timestamp); 71 128 statement.setString(2, sender); 72 129 statement.setString(3, rec); … … 110 167 if (receiverDecrypted.compareToIgnoreCase(name) == 0) { 111 168 c.init(Cipher.DECRYPT_MODE, key); 112 String timestamp = new String(c.doFinal(Base64.getDecoder().decode(mail.timestamp)));113 c.init(Cipher.DECRYPT_MODE, key);114 169 String sender = new String(c.doFinal(Base64.getDecoder().decode(mail.sender))); 115 170 c.init(Cipher.DECRYPT_MODE, key); … … 128 183 129 184 //String arguments = new String(c.doFinal(Base64.getDecoder().decode(mail.args))); 130 filtered.add(new MailEntry( timestamp, sender, receiverDecrypted, message, message2, options));185 filtered.add(new MailEntry(mail.timestamp, sender, receiverDecrypted, message, message2, options)); 131 186 } 132 187 }
Note:
See TracChangeset
for help on using the changeset viewer.