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)); } }
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 }