コード例 #1
0
        private void button_genResponse_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox_challengeValue.Text))
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    snackbar.MessageQueue.Enqueue("未收到挑战值,无法计算");
                }));
                return;
            }
            //如果已经认证通过就不需要再验证了
            if (IsAuthenticated)
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    snackbar.MessageQueue.Enqueue("已认证通过,无需再次操作");
                }));
                return;
            }

            StringBuilder sBuilder = new StringBuilder();

            byte[] hash;
            using (MD5 md5Hash = MD5.Create())
            {
                //计算响应值
                hash = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(textBox_challengeValue.Text));
                hash = md5Hash.ComputeHash(hash);
            }

            //计算响应密文(期望响应)
            AES = DataCrypto.GenAesCryptoServiceProvider(Key);
            string Response = Convert.ToBase64String(DataCrypto.Encrypt(hash, AES));

            textBox_genResponse.Text = Response;

            Authen_Message am = new Authen_Message(Status_Flag.Response_Challenge, DateTime.Now, Response);

            socketConn.Send(Message2Byte(am));
        }
コード例 #2
0
        private void Authentication()
        {
            while (true)
            {
                int    length = -1;
                byte[] buffer = new byte[Command_Buffer_Size];
                try
                {
                    length = socketConn.Receive(buffer);
                }
                catch (Exception ex)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        WriteLog(ex.ToString());
                        WriteLog("连接已断开");
                        snackbar.MessageQueue.Enqueue("连接已断开,请重新连接至服务");

                        button_connect.IsEnabled = true;
                        button_connect.Content   = "尝试连接";

                        ResetAuthenInfo();
                    }));
                    break;
                }

                byte[] message = new byte[length];
                Array.Copy(buffer, message, length);

                Authen_Message am = (Authen_Message)Byte2Message(message, "Authen_Message");

                this.Dispatcher.Invoke(new Action(() =>
                {
                    listBox_packetInfo.Items.Insert(0, am.MessageInfo());
                }));

                if (IsTimeOut(DateTime.Now))
                {
                    Authen_Message au = new Authen_Message(Status_Flag.Time_Out, DateTime.Now, null);
                    WriteLog("接收到超时的命令");
                }

                if (am.Flag == Status_Flag.Start_Challenge)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        textBox_challengeValue.Text = am.Extend;
                        WriteLog("收到挑战值" + am.Extend);
                        snackbar.MessageQueue.Enqueue("收到挑战值,请及时处理");
                    }));
                }

                if (am.Flag == Status_Flag.Authen_Success)
                {
                    IsAuthenticated = true;

                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        WriteLog("认证成功,密钥协商完毕,发送就绪");
                        snackbar.MessageQueue.Enqueue("认证成功");
                    }));

                    Thread trd = new Thread(TransControl);
                    trd.IsBackground = true;
                    trd.Start();

                    break;
                }

                if (am.Flag == Status_Flag.Authen_Failed)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        WriteLog("认证失败,请重试");
                        snackbar.MessageQueue.Enqueue("认证失败");
                    }));
                }
            }
        }
コード例 #3
0
ファイル: Command.cs プロジェクト: wongcong/Transmitor
 //将TcP包的内容转换为byte
 public static byte[] Message2Byte(Authen_Message am)
 {
     return(ByteConvertHelper.Object2Bytes(am));
 }