Browse Source

Added encrypting and a few commands.

Allexit 4 years ago
parent
commit
98cc5f7000

+ 2
- 1
.gitignore View File

@@ -36,4 +36,5 @@ nb-configuration.xml
36 36
 .nb-gradle/
37 37
 
38 38
 ### Private files ###
39
-serverproperties.json
39
+serverproperties.json
40
+clientproperties.json

+ 6
- 1
exampleproperties.json View File

@@ -1,5 +1,10 @@
1 1
 "Example-serverproperties.json":
2 2
 {
3 3
     "bank-name": "Bank name here",
4
-    "currency-name": "Currency name here"
4
+    "currency-name": "Currency name here",
5
+    "encrypt-key": "FD7EFACC2336E5EF"
6
+},
7
+"Example-clientproperties.json":
8
+{
9
+    "encrypt-key": "F9A8CB7E10FE13AC"
5 10
 }

+ 33
- 5
src/com/saltosion/neptulon/ClientMain.java View File

@@ -6,10 +6,12 @@ import com.saltosion.neptulon.net.ClientListener;
6 6
 import com.saltosion.neptulon.net.NetRegisterer;
7 7
 import com.saltosion.neptulon.net.PackageDistributor;
8 8
 import com.saltosion.neptulon.net.PackageHandler;
9
-import com.saltosion.neptulon.net.packages.BankInformation;
9
+import com.saltosion.neptulon.net.packages.BankInformationPackage;
10 10
 import com.saltosion.neptulon.net.packages.NetPackage;
11 11
 import com.saltosion.neptulon.ui.ClientTBUICreator;
12 12
 import com.saltosion.neptulon.ui.UI;
13
+import com.saltosion.neptulon.util.ClientProperties;
14
+import com.saltosion.neptulon.util.JsonLoader;
13 15
 import java.io.IOException;
14 16
 import java.util.ArrayList;
15 17
 import java.util.logging.Level;
@@ -24,6 +26,9 @@ public class ClientMain implements PackageHandler {
24 26
 	
25 27
 	private String bankName = "";
26 28
 	private String currencyName = "";
29
+	private boolean connectedToBank = false;
30
+	
31
+	private ClientProperties properties;
27 32
 	
28 33
 	/**
29 34
 	 * Initializes the Client and the UI for client. Necessary to run only once.
@@ -39,8 +44,11 @@ public class ClientMain implements PackageHandler {
39 44
 		currentUI = creator.create();
40 45
 		
41 46
 		//Initialize PackageDistributors
42
-		PackageDistributor.addHandlerFor(this, BankInformation.class);
43
-		PackageDistributor.addHandlerFor(creator, BankInformation.class);
47
+		PackageDistributor.addHandlerFor(this, BankInformationPackage.class);
48
+		PackageDistributor.addHandlerFor(creator, BankInformationPackage.class);
49
+		
50
+		//Initialize properties
51
+		properties = JsonLoader.loadClientProperties();
44 52
 		
45 53
 		currentUI.create();
46 54
 	}
@@ -77,15 +85,35 @@ public class ClientMain implements PackageHandler {
77 85
 		}
78 86
 		client.stop();
79 87
 		client = null;
88
+		connectedToBank = false;
80 89
 		return true;
81 90
 	}
91
+	
92
+	public BankInformationPackage getBankInformation() {
93
+		if (!connectedToBank) {
94
+			return null;
95
+		}
96
+		BankInformationPackage bi = new BankInformationPackage();
97
+		bi.bankName = bankName;
98
+		bi.currencyName = currencyName;
99
+		return bi;
100
+	}
101
+	
102
+	public boolean isConnectedToBank() {
103
+		return this.connectedToBank;
104
+	}
105
+	
106
+	public ClientProperties getProperties() {
107
+		return properties;
108
+	}
82 109
 
83 110
 	@Override
84 111
 	public void handle(NetPackage obj, Connection connection) {
85
-		if (obj instanceof BankInformation) {
86
-			BankInformation information = (BankInformation) obj;
112
+		if (obj instanceof BankInformationPackage) {
113
+			BankInformationPackage information = (BankInformationPackage) obj;
87 114
 			bankName = information.bankName;
88 115
 			currencyName = information.currencyName;
116
+			connectedToBank = true;
89 117
 		}
90 118
 	}
91 119
 	

+ 1
- 1
src/com/saltosion/neptulon/net/NetRegisterer.java View File

@@ -8,7 +8,7 @@ public class NetRegisterer {
8 8
 	
9 9
 	public static void registerClasses(Kryo kryo) {
10 10
 		kryo.register(NetPackage.class);
11
-		kryo.register(BankInformation.class);
11
+		kryo.register(BankInformationPackage.class);
12 12
 	}
13 13
 	
14 14
 }

+ 2
- 2
src/com/saltosion/neptulon/net/ServerListener.java View File

@@ -3,7 +3,7 @@ package com.saltosion.neptulon.net;
3 3
 import com.esotericsoftware.kryonet.Connection;
4 4
 import com.esotericsoftware.kryonet.Listener;
5 5
 import com.saltosion.neptulon.ServerMain;
6
-import com.saltosion.neptulon.net.packages.BankInformation;
6
+import com.saltosion.neptulon.net.packages.BankInformationPackage;
7 7
 import com.saltosion.neptulon.net.packages.NetPackage;
8 8
 import java.util.logging.Level;
9 9
 import java.util.logging.Logger;
@@ -21,7 +21,7 @@ public class ServerListener extends Listener {
21 21
 	@Override
22 22
 	public void connected(Connection cnctn) {
23 23
 		logger.log(Level.INFO, "Someone entered the bank! (ID: {0})", cnctn.getID());
24
-		BankInformation information = new BankInformation();
24
+		BankInformationPackage information = new BankInformationPackage();
25 25
 		information.bankName = serverMain.getProperties().getBankName();
26 26
 		information.currencyName = serverMain.getProperties().getCurrencyName();
27 27
 		serverMain.getServer().sendToTCP(cnctn.getID(), information);

src/com/saltosion/neptulon/net/packages/BankInformation.java → src/com/saltosion/neptulon/net/packages/BankInformationPackage.java View File

@@ -1,6 +1,6 @@
1 1
 package com.saltosion.neptulon.net.packages;
2 2
 
3
-public class BankInformation extends NetPackage {
3
+public class BankInformationPackage extends NetPackage {
4 4
 	
5 5
 	public String bankName;
6 6
 	public String currencyName;

+ 7
- 0
src/com/saltosion/neptulon/net/packages/LoginPackage.java View File

@@ -0,0 +1,7 @@
1
+package com.saltosion.neptulon.net.packages;
2
+
3
+public class LoginPackage extends NetPackage {
4
+	public String username;
5
+	public String password;
6
+	
7
+}

+ 46
- 3
src/com/saltosion/neptulon/ui/ClientTBUICreator.java View File

@@ -1,13 +1,15 @@
1 1
 package com.saltosion.neptulon.ui;
2 2
 
3
+import com.saltosion.neptulon.util.Encrypter;
3 4
 import com.esotericsoftware.kryonet.Connection;
4 5
 import com.saltosion.neptulon.ClientMain;
5 6
 import com.saltosion.neptulon.net.PackageHandler;
6
-import com.saltosion.neptulon.net.packages.BankInformation;
7
+import com.saltosion.neptulon.net.packages.BankInformationPackage;
7 8
 import com.saltosion.neptulon.net.packages.NetPackage;
8 9
 import com.saltosion.neptulon.ui.textbasedcommands.Command;
9 10
 import com.saltosion.neptulon.ui.textbasedcommands.CommandManager;
10 11
 import java.io.IOException;
12
+import java.security.GeneralSecurityException;
11 13
 import java.util.ArrayList;
12 14
 import java.util.logging.Level;
13 15
 import java.util.logging.Logger;
@@ -90,6 +92,47 @@ public class ClientTBUICreator implements UICreator<TextBasedUI>, PackageHandler
90 92
 							
91 93
 		});
92 94
 		
95
+		commandManager.getCommandList().add(new Command("bankinformation", 
96
+						"Gives information about bank currently connected to.", 
97
+						"Gives name and currency name of the current connected bank.") {
98
+			@Override
99
+			public void run(CommandManager manager, ArrayList<String> args) {
100
+				BankInformationPackage bi = clientMain.getBankInformation();
101
+				if (bi == null) {
102
+					logger.log(Level.WARNING, "I'm not in a bank right now!");
103
+					return;
104
+				}
105
+				logger.log(Level.INFO, "I think the bank was called ''{0}'', and the currency was called ''{1}''",
106
+						new Object[]{bi.bankName, bi.currencyName});
107
+			}
108
+			
109
+		});
110
+		
111
+		commandManager.getCommandList().add(new Command("login", 
112
+						"logins the user to the bank in the specified account.",
113
+						"logs the user in, usage: login <username> <password>") {
114
+
115
+			@Override
116
+			public void run(CommandManager manager, ArrayList<String> args) {
117
+				if (args.size() < 2) {
118
+					logger.log(Level.WARNING, "Too few arguments! try 'help login'!");
119
+				}
120
+				String username = args.get(0);
121
+				String password = args.get(1);
122
+				String encryptedPassword = "";
123
+				try {
124
+					encryptedPassword = Encrypter.encryptString(password, 
125
+						clientMain.getProperties().getEncryptKey());
126
+				} catch (GeneralSecurityException e) {
127
+					logger.log(Level.INFO, e.getMessage());
128
+					return;
129
+				}
130
+				
131
+				logger.log(Level.INFO, "{0}", encryptedPassword);
132
+			}
133
+							
134
+		});
135
+		
93 136
 		TextBasedUIGeneralCommands.giveGeneralCommands(commandManager, logger, ui);
94 137
 		
95 138
 		commandManager.getCommand("clear").run(commandManager, null);
@@ -100,8 +143,8 @@ public class ClientTBUICreator implements UICreator<TextBasedUI>, PackageHandler
100 143
 
101 144
 	@Override
102 145
 	public void handle(NetPackage obj, Connection connection) {
103
-		if (obj instanceof BankInformation) {
104
-			BankInformation information = (BankInformation) obj;
146
+		if (obj instanceof BankInformationPackage) {
147
+			BankInformationPackage information = (BankInformationPackage) obj;
105 148
 			TextBasedUI.logger.log(Level.INFO, "Entered {0}, and changed used currency to {1}",
106 149
 					new Object[]{information.bankName, information.currencyName});
107 150
 		}

+ 0
- 2
src/com/saltosion/neptulon/ui/TextBasedUI.java View File

@@ -1,9 +1,7 @@
1 1
 
2 2
 package com.saltosion.neptulon.ui;
3 3
 
4
-import com.esotericsoftware.kryonet.Connection;
5 4
 import com.saltosion.neptulon.net.PackageHandler;
6
-import com.saltosion.neptulon.net.packages.NetPackage;
7 5
 import com.saltosion.neptulon.ui.textbasedcommands.CommandManager;
8 6
 import java.util.ArrayList;
9 7
 import java.util.Scanner;

+ 24
- 0
src/com/saltosion/neptulon/ui/TextBasedUIGeneralCommands.java View File

@@ -2,6 +2,7 @@ package com.saltosion.neptulon.ui;
2 2
 
3 3
 import com.saltosion.neptulon.ui.textbasedcommands.Command;
4 4
 import com.saltosion.neptulon.ui.textbasedcommands.CommandManager;
5
+import com.saltosion.neptulon.util.Encrypter;
5 6
 import java.util.ArrayList;
6 7
 import java.util.logging.Level;
7 8
 import java.util.logging.Logger;
@@ -76,6 +77,29 @@ public class TextBasedUIGeneralCommands {
76 77
 			}
77 78
 					
78 79
 		});
80
+		
81
+		commandManager.getCommandList().add(
82
+				new Command("generatekey", 
83
+						"Generates a hex-key that can be used for encryption.",
84
+						"Usage: generatekey <bitamount> (bitamount must be in power of 2)") {
85
+
86
+			@Override
87
+			public void run(CommandManager manager, ArrayList<String> args) {
88
+				if (args.size() < 1) {
89
+					logger.log(Level.WARNING, "Too few arguments! Try 'help generatekey' !");
90
+					return;
91
+				}
92
+				int byteAmount = 0;
93
+				try {
94
+					byteAmount = Integer.parseInt(args.get(0));
95
+				} catch(NumberFormatException e) {
96
+					logger.log(Level.SEVERE, e.getMessage());
97
+					return;
98
+				}
99
+				logger.log(Level.INFO, Encrypter.generateHexKey(byteAmount));
100
+			}
101
+							
102
+		});
79 103
 	}
80 104
 	
81 105
 }

+ 6
- 0
src/com/saltosion/neptulon/util/BankProperties.java View File

@@ -7,6 +7,7 @@ public class BankProperties implements Json.Serializable {
7 7
 
8 8
 	private String bankName;
9 9
 	private String currencyName;
10
+	private String encryptKey;
10 11
 	
11 12
 	public String getBankName() {
12 13
 		return this.bankName;
@@ -16,6 +17,10 @@ public class BankProperties implements Json.Serializable {
16 17
 		return this.currencyName;
17 18
 	}
18 19
 	
20
+	public String getEncryptKey() {
21
+		return this.encryptKey;
22
+	}
23
+	
19 24
 	@Override
20 25
 	public void write(Json json) {
21 26
 	}
@@ -24,6 +29,7 @@ public class BankProperties implements Json.Serializable {
24 29
 	public void read(Json json, JsonValue jv) {
25 30
 		this.bankName = jv.require("bank-name").asString();
26 31
 		this.currencyName = jv.require("currency-name").asString();
32
+		this.encryptKey = jv.require("encrypt-key").asString();
27 33
 	}
28 34
 	
29 35
 	

+ 23
- 0
src/com/saltosion/neptulon/util/ClientProperties.java View File

@@ -0,0 +1,23 @@
1
+package com.saltosion.neptulon.util;
2
+
3
+import com.esotericsoftware.jsonbeans.Json;
4
+import com.esotericsoftware.jsonbeans.JsonValue;
5
+
6
+public class ClientProperties implements Json.Serializable {
7
+	
8
+	private String encryptKey = "";
9
+	
10
+	public String getEncryptKey() {
11
+		return this.encryptKey;
12
+	}
13
+
14
+	@Override
15
+	public void write(Json json) {
16
+	}
17
+
18
+	@Override
19
+	public void read(Json json, JsonValue jv) {
20
+		encryptKey = jv.require("encrypt-key").asString();
21
+	}
22
+	
23
+}

+ 53
- 0
src/com/saltosion/neptulon/util/Encrypter.java View File

@@ -0,0 +1,53 @@
1
+package com.saltosion.neptulon.util;
2
+
3
+import java.security.InvalidKeyException;
4
+import java.security.Key;
5
+import java.security.NoSuchAlgorithmException;
6
+import java.util.Base64;
7
+import java.util.logging.Logger;
8
+import javax.crypto.BadPaddingException;
9
+import javax.crypto.Cipher;
10
+import javax.crypto.IllegalBlockSizeException;
11
+import javax.crypto.NoSuchPaddingException;
12
+import javax.crypto.spec.SecretKeySpec;
13
+import javax.xml.bind.DatatypeConverter;
14
+
15
+public class Encrypter {
16
+	
17
+	private static Logger logger = Logger.getLogger(Encrypter.class.getName());
18
+	
19
+	public static String encryptString(String password, String keyString) throws NoSuchAlgorithmException, 
20
+			InvalidKeyException, 
21
+			NoSuchPaddingException, 
22
+			IllegalBlockSizeException, 
23
+			BadPaddingException {
24
+		byte[] keyBytes = DatatypeConverter.parseHexBinary(keyString);
25
+		Key key = new SecretKeySpec(keyBytes, "AES");
26
+		Cipher c = Cipher.getInstance("AES");
27
+		
28
+		c.init(Cipher.ENCRYPT_MODE, key);		
29
+		byte[] encryptBytes = c.doFinal(password.getBytes());
30
+		
31
+		return Base64.getEncoder().encodeToString(encryptBytes);
32
+	}
33
+	
34
+	/**
35
+	 * Generates a Hex -string that can be used as a key. 
36
+	 * @param byteAmount amount of bytes (must be in power of 2)
37
+	 * @return 
38
+	 */
39
+	public static String generateHexKey(int byteAmount) {
40
+		double check = Math.log10(byteAmount)/Math.log10(2);
41
+		if (check != (int)check) {
42
+			return "InvalidKey";
43
+		}
44
+		char[] chars = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', 
45
+			'9', 'A', 'B', 'C', 'D', 'E', 'F'};
46
+		String hex = "";
47
+		for (int i=0; i<byteAmount; i++) {
48
+			int index = (int) Math.round(Math.random()*(chars.length-1));
49
+			hex += chars[index];
50
+		}
51
+		return hex;
52
+	}
53
+}

+ 6
- 0
src/com/saltosion/neptulon/util/JsonLoader.java View File

@@ -17,6 +17,12 @@ public class JsonLoader {
17 17
 		return json.fromJson(BankProperties.class, s);
18 18
 	}
19 19
 	
20
+	public static ClientProperties loadClientProperties() {
21
+		Json json = new Json();
22
+		String s = getJsonString("clientproperties.json");
23
+		return json.fromJson(ClientProperties.class, s);
24
+	}
25
+	
20 26
 	private static String getJsonString(String filename) {
21 27
 		Scanner reader;
22 28
 		try {