scp-app

git clone git://git.codymlewis.com/scp-app.git
Log | Files | Refs | LICENSE

commit 3c34f4ad5d3fe5c4f154fa7e22f243182b57d172
parent e93e3d5f2f88e21353695f0987fbdaabf8365d9c
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Mon,  3 Sep 2018 08:38:30 +1000

Cleaned some exception handling

Diffstat:
MAES.java | 61++++++++++++++++++++++++++++++++++++++-----------------------
MChat.java | 7+------
MChatClient.java | 3++-
MChatServer.java | 5+++--
4 files changed, 44 insertions(+), 32 deletions(-)

diff --git a/AES.java b/AES.java @@ -16,9 +16,14 @@ public class AES { * @param key a secret key * @return the hash of the secret key */ - private static byte[] hashKey(byte[] key) throws Exception { - MessageDigest md = MessageDigest.getInstance("SHA-256"); - return md.digest(key); + private static byte[] hashKey(byte[] key) { + try { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + return md.digest(key); + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + return new byte[0]; + } } /** * Perform AES encryption @@ -26,20 +31,25 @@ public class AES { * @param message the plaintext * @return cipher-text of the message */ - public static String[] encrypt(byte[] sessionKey, String message) throws Exception { - // set up cipher - Cipher c = Cipher.getInstance(CIPHER_ALGORITHM); - SecretKeySpec secret = new SecretKeySpec(hashKey(sessionKey), "AES"); - c.init(c.ENCRYPT_MODE, secret); - // produce ciphertext - int len = message.length() / 16 + ((message.length() % 16 == 0) ? 0 : 1); - String[] ciphertext = new String[len]; - String[] result = new String[len]; - for(int i = 0; i < len; ++i) { - ciphertext[i] = (i + 1) * 16 > message.length() ? message.substring(i * 16) : message.substring(i * 16, (i + 1) * 16); - result[i] = Base64.getEncoder().encodeToString(c.doFinal(ciphertext[i].getBytes("UTF-8"))); + public static String[] encrypt(byte[] sessionKey, String message) { + try { + // set up cipher + Cipher c = Cipher.getInstance(CIPHER_ALGORITHM); + SecretKeySpec secret = new SecretKeySpec(hashKey(sessionKey), "AES"); + c.init(c.ENCRYPT_MODE, secret); + // produce ciphertext + int len = message.length() / 16 + ((message.length() % 16 == 0) ? 0 : 1); + String[] ciphertext = new String[len]; + String[] result = new String[len]; + for(int i = 0; i < len; ++i) { + ciphertext[i] = (i + 1) * 16 > message.length() ? message.substring(i * 16) : message.substring(i * 16, (i + 1) * 16); + result[i] = Base64.getEncoder().encodeToString(c.doFinal(ciphertext[i].getBytes("UTF-8"))); + } + return result; + } catch(Exception e) { + System.err.println("Error: " + e.getMessage()); + return new String[0]; } - return result; } /** * Perform AES decryption @@ -47,12 +57,17 @@ public class AES { * @param ciphertext some AES encrypted text * @return the plain-text */ - public static String decrypt(byte[] sessionKey, String ciphertext) throws Exception { - // set up cipher - Cipher c = Cipher.getInstance(CIPHER_ALGORITHM); - SecretKeySpec secret = new SecretKeySpec(hashKey(sessionKey), "AES"); - c.init(c.DECRYPT_MODE, secret); - // produce plaintext - return new String(c.doFinal(Base64.getDecoder().decode(ciphertext.substring(0, ciphertext.indexOf("\n") == -1 ? ciphertext.length() : ciphertext.indexOf("\n"))))); + public static String decrypt(byte[] sessionKey, String ciphertext) { + try { + // set up cipher + Cipher c = Cipher.getInstance(CIPHER_ALGORITHM); + SecretKeySpec secret = new SecretKeySpec(hashKey(sessionKey), "AES"); + c.init(c.DECRYPT_MODE, secret); + // produce plaintext + return new String(c.doFinal(Base64.getDecoder().decode(ciphertext.substring(0, ciphertext.indexOf("\n") == -1 ? ciphertext.length() : ciphertext.indexOf("\n"))))); + } catch(Exception e) { + System.err.println("Error: " + e.getMessage()); + return new String(); + } } } \ No newline at end of file diff --git a/Chat.java b/Chat.java @@ -168,12 +168,7 @@ public class Chat extends JFrame { return "ACKNOWLEDGE"; } String message = SCP.parseMessage(packet, address.getHostAddress(), port); - try { - return AES.decrypt(sessionKey.toByteArray(), message); - } catch(Exception e) { - System.err.println("Error: " + e.getMessage()); - return ""; - } + return AES.decrypt(sessionKey.toByteArray(), message); } /** * Disconnection event handler diff --git a/ChatClient.java b/ChatClient.java @@ -8,12 +8,13 @@ import java.net.InetAddress; import java.net.UnknownHostException; /** * ChatClient.java - * A socket based half duplex chat client + * A socket based full duplex chat client * * @author Cody Lewis * @since 2018-08-10 */ public class ChatClient extends Chat { + public static final long serialVersionUID = 1L; public ChatClient(String title) { super(title); } /** * The main thread diff --git a/ChatServer.java b/ChatServer.java @@ -8,12 +8,13 @@ import java.net.Socket; import java.net.InetAddress; /** * ChatServer.java - * A socket based half duplex chat server + * A socket based full duplex chat server * * @author Cody Lewis * @since 2018-08-10 */ public class ChatServer extends Chat { + public static final long serialVersionUID = 1L; private ServerSocket serverSocket; private String username; private String welcomeMessage; @@ -65,7 +66,7 @@ public class ChatServer extends Chat { msgArea.append("Waiting for client to connect\n"); acceptClient(); msgArea.append("Client successfully connected\n"); - msgArea.append("Exchanging Keys"); + msgArea.append("Exchanging Keys\n"); keyExchange(); msgArea.append("Waiting for client to SCP connect\n"); username = clientConnect();