public static int Establish(ref byte[] EncryptionKeyServer, ref byte[] Salt, HenkTcpClient Client, string Password, string UserName, RSAKey RSAKey) { try { EncryptionKeyServer = Encryption.RSA.Decrypt(Client.WriteAndGetReply(CombineBytes(new byte[] { 42, 1 }, RSAKey.PublicKey), TimeSpan.FromSeconds(TIMESPAN)).Data, RSAKey.PrivateKey); Client.SetEncryption(Aes.Create(), EncryptionKeyServer); Salt = Client.WriteAndGetReply(new byte[] { 42, 2 }, TimeSpan.FromSeconds(1)).DecryptedData; Rfc2898DeriveBytes HashedPassword = new Rfc2898DeriveBytes(Password, Salt, 250000); byte ValidPassword = Client.WriteAndGetReply(CombineBytes(new byte[] { 42, 3 }, HenkTcp.Encryption.Encrypt(Aes.Create(), HashedPassword.GetBytes(20), EncryptionKeyServer)), TimeSpan.FromSeconds(TIMESPAN)).Data[0]; if (ValidPassword.Equals(1)) { byte ValidUserName = Client.WriteAndGetReply(CombineBytes(new byte[] { 42, 4 }, AES256.encrypt(UserName, Password, Salt)), TimeSpan.FromSeconds(TIMESPAN)).Data[0]; if (ValidUserName.Equals(1)) { return(3); //evrything ok } else { Client.Disconnect(); return(2); } //UserName already taken } else { Client.Disconnect(); return(1); } //Wrong password } catch { return(0); }//server did not reply on a message }
public static string SendCommand(string Text, HenkTcpClient Client, byte[] EncryptionKeyServer, string Password, byte[] Salt) { if (Text.Equals("!users")) { int Online = BitConverter.ToInt32(Client.WriteAndGetReply(new byte[] { 42, 6, 1 }, TimeSpan.FromSeconds(1)).DecryptedData, 0); string Users = string.Empty; for (int x = 0; x < Online; x++) { Users += ", " + AES256.decrypt(Client.WriteAndGetReply(CombineBytes(new byte[] { 42, 6, 1 }, BitConverter.GetBytes(x)), TimeSpan.FromSeconds(1)).Data, Password, Salt); } return($"({Online}) {Users.Remove(0, 2)}"); } if (Text.StartsWith("!admin ")) { Rfc2898DeriveBytes HashedAdminPassword = new Rfc2898DeriveBytes(Text.Remove(0, 7), Salt, 500000); return(_SendCommand(CombineBytes(new byte[] { 42, 6 }, HenkTcp.Encryption.Encrypt(Aes.Create(), Encoding.UTF8.GetBytes("!admin " + Convert.ToBase64String(HashedAdminPassword.GetBytes(20))), EncryptionKeyServer)), Client)); } else if (Text.StartsWith("!kick ")) { return(_SendCommand(CombineBytes(new byte[] { 42, 6, 2 }, AES256.encrypt(Text.Remove(0, 6), Password, Salt)), Client)); } else if (Text.StartsWith("!ban ")) { return(_SendCommand(CombineBytes(new byte[] { 42, 6, 3 }, AES256.encrypt(Text.Remove(0, 5), Password, Salt)), Client)); } else { return(_SendCommand(CombineBytes(new byte[] { 42, 6 }, HenkTcp.Encryption.Encrypt(Aes.Create(), Encoding.UTF8.GetBytes(Text), EncryptionKeyServer)), Client)); } }
private async void _OnDisconnect(object sender, HenkTcpClient e) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Chat_GRD.Visibility = Visibility.Collapsed; Connect_GRD.Visibility = Visibility.Visible; Messages_LV.Items.Clear(); Message_TB.Text = string.Empty; _Client.DataReceived -= _DataReceived; _Client.OnDisconnect -= _OnDisconnect; }); }
private void _Connect(object sender, RoutedEventArgs e) { if (Popup_GRD.Visibility == Visibility.Visible) { Popup_Text.Text = "You are trying to quick"; return; } Settings.SetIp(ServerName_TB.Text); Settings.SetUsername(UserName_TB.Text); _Client = new HenkTcpClient(); int x = Connection.Connect(ServerName_TB.Text, _Client); if (x.Equals(0)) { _ShowPopup("Could not connect to NameServer"); return; } else if (x.Equals(1)) { _ShowPopup("Server does not exists"); return; } else if (x.Equals(2)) { _ShowPopup("Could not connect to server"); return; } x = Connection.Establish(ref EncryptionKeyServer, ref Salt, _Client, Password_PWB.Password, UserName_TB.Text, _RSAKey); if (x.Equals(0)) { _ShowPopup("Server timed out"); } else if (x.Equals(1)) { _ShowPopup("Wrong password"); } else if (x.Equals(2)) { _ShowPopup("UserName is already taken"); } else if (x.Equals(3)) { _Connected(); } }
private static string _SendCommand(byte[] Data, HenkTcpClient Client) => Encoding.UTF8.GetString(Client.WriteAndGetReply(Data, TimeSpan.FromSeconds(1)).DecryptedData);
public static int Connect(string ServerName, HenkTcpClient Client) { if (ServerName.StartsWith("!")) { string[] Credentials = ServerName.Split('/'); if (Credentials.Length.Equals(2)) { int Port; if (!int.TryParse(Credentials[1], out Port)) { return(2); } return(Client.Connect(Credentials[0].Remove(0, 1), Port, TimeSpan.FromSeconds(TIMESPAN)) ? 3 : 2);//2 = could not connect to server,3= ok } else if (Credentials.Length.Equals(3)) { int Port; if (!int.TryParse(Credentials[1], out Port)) { return(2); } if (!Client.Connect(Credentials[0].Remove(0, 1), Port, TimeSpan.FromSeconds(TIMESPAN))) { return(0); //could not connect to name server } var Reply = Client.WriteAndGetReply(new Rfc2898DeriveBytes(Credentials[2].ToLower(), new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, 10000).GetBytes(20), TimeSpan.FromSeconds(TIMESPAN)); if (Reply == null) { return(0); } else if (Reply.Data[0].Equals(0)) { return(1); } int ServerPort = BitConverter.ToInt32(Reply.Data, 0); return(Client.Connect(Credentials[0].Remove(0, 1), ServerPort, TimeSpan.FromSeconds(TIMESPAN)) ? 3 : 2);//2 = could not connect to server,3= ok } else { return(2);//could not connect to server, invalid credentials } } else { if (!Client.Connect(IP, NAME_SERVER_PORT, TimeSpan.FromSeconds(TIMESPAN))) { return(0); //could not connect to name server } var Reply = Client.WriteAndGetReply(new Rfc2898DeriveBytes(ServerName.ToLower(), new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, 10000).GetBytes(20), TimeSpan.FromSeconds(TIMESPAN)); if (Reply == null) { return(0); } else if (Reply.Data[0].Equals(0)) { return(1); } int ServerPort = BitConverter.ToInt32(Reply.Data, 0); return(Client.Connect(IP, ServerPort, TimeSpan.FromSeconds(TIMESPAN)) ? 3 : 2);//2 = could not connect to server,3= ok } }