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 }
private void _Send(object sender, RoutedEventArgs e) { if (Message_TB.Text.Length < 2) { return; } try { if (Message_TB.Text.StartsWith("!")) { if (Message_TB.Text.Equals("!leave")) { _Client.Disconnect(true); } else { _Client.DataReceived -= _DataReceived; Messages_LV.Items.Add(Functions.CreateMessageBox(Connection.SendCommand(Message_TB.Text, _Client, EncryptionKeyServer, Password_PWB.Password, Salt), UserName_TB.Text)); _Client.DataReceived += _DataReceived; } } else { byte[] Message = HenkTcp.Encryption.Encrypt(Aes.Create(), Encoding.UTF8.GetBytes(UserName_TB.Text + ":" + Message_TB.Text), EndToEndKey); if (Message.Length > 1024) { _ShowPopup("Message is too long"); return; } _Client.Write(Message); } Message_TB.Text = string.Empty; } catch { _OnDisconnect(null, null); } }