Exemple #1
0
        public bool Handshake01(string UserName, string Password)
        {
            var aes = Aes.Create(); aes.GenerateKey(); aes.GenerateIV();

            layer = new AESLayer(aes.Key, aes.IV, s);
            var rsa = RSA.Create();

            rsa.ImportRSAPublicKey(RSAPubKey, out _);
            {
                //Send AES Key
                //byte[] Part1 = new byte[190];
                //for (int i = 0; i < 32; i++)
                //{
                //    Part1[i] = aes.Key[i];
                //}
                var FData0 = rsa.Encrypt(aes.Key, RSAEncryptionPadding.OaepSHA256);
                s.Send(FData0, 256, SocketFlags.None);
            }
            //{
            //    //Send AES Key
            //    byte[] Part2 = new byte[66];
            //    for (int i = 0; i < 66; i++)
            //    {
            //        Part2[i] = aes.Key[i + 190];
            //    }
            //    var FData1 = rsa.Encrypt(Part2, RSAEncryptionPadding.OaepSHA256);
            //    s.Send(FData1, 256, SocketFlags.None);
            //}
            {
                //Send AES IV
                var FinalEncIV = rsa.Encrypt(aes.IV, RSAEncryptionPadding.OaepSHA256);
                s.Send(FinalEncIV, 256, SocketFlags.None);
            }
            {
                //Send U/N
                layer.Write(Encoding.UTF8.GetBytes(UserName));
                layer.Write(Encoding.UTF8.GetBytes(Password));
            }
            {
                byte[] receieved = null;
                layer.Read(out receieved);
                if (receieved.Length >= 2)
                {
                    if (receieved.Length is 2 && receieved[0] == 'N' && receieved[1] == 'O')
                    {
                        return(false);
                    }
                    else
                    {
                        Auth = Encoding.UTF8.GetString(receieved);
                        Task.Run(Listen);
                        return(true);
                    }
                }
Exemple #2
0
        void Listen()
        {
            //try
            //{
            while (Stop is not true)
            {
                try
                {
                    int Operation = -1;
                    {
                        layer.Read(out byte[] d);
                        Operation = BitConverter.ToInt32(d);
                    }
                    {
                        switch (Operation)
                        {
                        case 0:
                        {
                            layer.Read(out byte[] d);
                            string msg = Encoding.UTF8.GetString(d);
                            foreach (var item in Outputs)
                            {
                                item.Write(msg);
                            }
                        }
                        break;

                        case 1:
                        {
                            layer.Read(out byte[] d);
                            string msg = Encoding.UTF8.GetString(d);
                            foreach (var item in Outputs)
                            {
                                item.WriteLine(msg);
                            }
                        }
                        break;

                        case 2:
                        {
                            layer.Read(out _);
                            foreach (var item in Outputs)
                            {
                                item.Flush();
                            }
                        }
                        break;

                        case 3:
                        {
                            try
                            {
                                layer.Read(out byte[] d);
                                string msg   = Encoding.UTF8.GetString(d);
                                var    color = Enum.Parse <ConsoleColor>(msg);
                                foreach (var item in Outputs)
                                {
                                    item.SetForeground(color);
                                }
                            }
                            catch
                            {
                            }
                        }
                        break;

                        case 4:
                        {
                            try
                            {
                                layer.Read(out byte[] d);
                                string msg   = Encoding.UTF8.GetString(d);
                                var    color = Enum.Parse <ConsoleColor>(msg);
                                foreach (var item in Outputs)
                                {
                                    item.SetBackground(color);
                                }
                            }
                            catch
                            {
                            }
                        }
                        break;

                        case 5:
                        {
                            layer.Read(out _);
                            foreach (var item in Outputs)
                            {
                                item.ResetColor();
                            }
                        }
                        break;

                        default:
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    if (s.Connected == false)
                    {
                        if (OnConnectionLost is not null)
                        {
                            OnConnectionLost();
                        }
                        return;
                    }
                }
            }
            //}
            //catch (Exception)
            //{
            //}
        }