Exemple #1
0
        private void Connect()
        {
            try
            {
                byte[] dataBuffer = new byte[1024];
                int    readCount  = 0;

                mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                mSocket.Connect(mHost, mPort);
                RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
                readCount = Socket.Receive(dataBuffer);
                string cryptoProviderPublicKey = Encoding.ASCII.GetString(dataBuffer, 0, readCount);
                rsaProvider.FromXmlString(cryptoProviderPublicKey);

                byte[] passwordData          = Encoding.ASCII.GetBytes(mPassword);
                byte[] passwordDataEncrypted = rsaProvider.Encrypt(passwordData, true);
                mSocket.Send(passwordDataEncrypted);

                readCount = mSocket.Receive(dataBuffer);
                string result = Encoding.ASCII.GetString(dataBuffer, 0, readCount);
                if (result != "OK")
                {
                    InvalidPasswordException exception = new InvalidPasswordException();
                    exception.Password      = mPassword;
                    exception.ServerMessage = result;
                    throw exception;
                }

                mCryptoServiceProvider = new TripleDESCryptoServiceProvider();
                byte[] keyData              = mCryptoServiceProvider.Key;
                byte[] ivData               = mCryptoServiceProvider.IV;
                byte[] typeData             = BitConverter.GetBytes(mConnectType);
                byte[] connectData          = BytesUtils.Merge(keyData, ivData, typeData);
                byte[] connectDataEncrypted = rsaProvider.Encrypt(connectData, true);
                mSocket.Send(connectDataEncrypted);

                readCount = mSocket.Receive(dataBuffer);
                result    = Encoding.ASCII.GetString(dataBuffer, 0, readCount);
                if (result != "OK")
                {
                    InvalidConnectInfoException exception = new InvalidConnectInfoException();
                    exception.ServerMessage = result;
                    throw exception;
                }

                ConnectSucceedArgs args = new ConnectSucceedArgs();
                mForm.Invoke(mConnectSucceedHandler, this, args);
            }
            catch (Exception ex)
            {
                mSocket.Close();
                mSocket = null;
                mCryptoServiceProvider = null;

                ConnectFailArgs args = new ConnectFailArgs();
                args.Reason = ex;
                mForm.Invoke(mConnectFailHandler, this, args);
            }
        }
Exemple #2
0
        private void OnConnectFail(object sender, ConnectFailArgs e)
        {
            mLoadingCover.Visible = false;

            mToolTip.ToolTipIcon  = ToolTipIcon.Error;
            mToolTip.ToolTipTitle = "连接失败";
            mToolTip.IsBalloon    = true;
            mToolTip.Show(e.Reason.Message, mConnectButton, 2000);
        }