public static int DatabaseAction(String[] commands) { Debug.Print("Executing database command."); Database curDatabase; if(commands[1].Equals(usrHead)) { Debug.Print("User database"); curDatabase = new Database("users"); } else if (commands[1].Equals(logHead)) { Debug.Print("Log database"); curDatabase = new Database("logs"); } else { return -1; } curDatabase.read(); if(commands[2].Equals(cmdAdd)) { ArrayList values = new ArrayList(); for (int i = 2; i < commands.Length; i++) { values.Add(commands[i]); } Encryption.RSAgenerateKeys(); values.Add(Encryption.getE()); values.Add(Encryption.getN()); curDatabase.add(values); String[] returnCommands = { "UsrAdded", "" + Encryption.getD(), "" + Encryption.getN() }; } else if(commands[2].Equals(cmdRem)) { curDatabase.remove(int.Parse(commands[2])); } else if(commands[2].Equals(cmdGet)) { curDatabase.get(commands[2],commands[3]); } else if (commands[2].Equals(cmdLogin)) { Debug.Print("Attempting to login."); ArrayList dataObjects = curDatabase.get("username", commands[3]); if (dataObjects.Count == 0) return -3; ArrayList user = (ArrayList)dataObjects[0]; byte[] tempBytePass = System.Text.Encoding.UTF8.GetBytes(commands[4]+"\0"); String password = SHA.encryptPassword(tempBytePass); if ((String)user[1] == password) return 15; return -3; } return 1; }
public static String[] UserAction(String[] commands) { ArrayList returnValues = new ArrayList(); String[] value = null; Database database = new Database("users"); database.read(); if (commands[0].Equals(usrHead)) { if(commands[1].Equals("ALL")) { Debug.Print("Getting User List"); returnValues = database.getAll(); value = new String[2]; value[0] = "USER"; for (int i = 0; i < 1; i++) { Debug.Print("Found user " + ((ArrayList)(returnValues[i]))[0].ToString()); value[i + 1] = ((ArrayList)(returnValues[i]))[0].ToString(); } } else if (commands[1].Equals("INFO")) { Debug.Print("Getting user information for user " + commands[2]); returnValues = database.get("username", commands[2]); if (returnValues.Count == 0) { Debug.Print("Unable to find the desired user"); return null; } value = new String[((ArrayList)returnValues[0]).Count+1]; value[0] = "URETURN"; for(int i = 0;i < ((ArrayList)returnValues[0]).Count;i++) value[i + 1] = ((ArrayList)(returnValues[0]))[i].ToString(); } else if (commands[1].Equals("CHANGE")) { String username = commands[2]; String password = commands[3]; String admin = commands[4]; Debug.Print(username + " " + password + " " + admin); returnValues = database.get("username", username); if (returnValues.Count == 0) { ArrayList temp2 = new ArrayList(); temp2.Add(username); String encrypted = SHA.encryptPassword(System.Text.Encoding.UTF8.GetBytes(password + "\0")); temp2.Add(encrypted); temp2.Add(admin); database.add(temp2); } else { ArrayList temp2 = ((ArrayList)(returnValues[0])); if (password != "null") { String encrypted = SHA.encryptPassword(System.Text.Encoding.UTF8.GetBytes(password + "\0")); temp2[1] = encrypted; } temp2[2] = admin; database.modify(database.getIndex("username", username), temp2); value = null; } } } return value; }
public static void Main() { Debug.Print("Start..."); ServerSocket.listen(8877); DatabaseGenerator.generateUsers(); DatabaseGenerator.generateLogs(); Encryption.init(); Encryption.RSAgenerateKeys(); Debug.Print(Encryption.toString()); Debug.Print("Generated..."); Database tempDatabase = new Database("users"); tempDatabase.read(); ArrayList temp2 = new ArrayList(); temp2.Add("IC"); String test = SHA.encryptPassword(System.Text.Encoding.UTF8.GetBytes("123\0")); //Debug.Print(SHA.byteToString(test)); temp2.Add(test); temp2.Add("false"); tempDatabase.add(temp2); while (true) { ArrayList cons = ServerSocket.getConnectionIDs(); foreach (Object con in cons) { if ((int)con != -1) { String temp; while ((temp = ServerSocket.getNextMessage((int)con)) != null) ServerSocket.sendMessage(-1, temp); } } } while (true) { } /* Database tempDatabase = new Database("users"); tempDatabase.read(); ArrayList temp2 = new ArrayList(); temp2.Add("IC"); String test = SHA.encryptPassword(System.Text.Encoding.UTF8.GetBytes("123\0")); //Debug.Print(SHA.byteToString(test)); temp2.Add(test); temp2.Add("false"); temp2.Add("1"); temp2.Add("2"); tempDatabase.add(temp2); ArrayList tempData = tempDatabase.get("username", "IC"); if (tempData.Count > 0) Debug.Print((String)tempData[0]); else Debug.Print("Unable to find any account"); */ }