Esempio n. 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));
        }
Esempio n. 2
0
        private void SendFile(object fileParam)
        {
            File_Info fi = (File_Info)fileParam;

            socketSendFile = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(((IPEndPoint)socketConn.RemoteEndPoint).Address, 8089);

            try
            {
                socketSendFile.Connect(iep);
            }
            catch
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    WriteLog("传输端口无响应");
                    snackbar.MessageQueue.Enqueue("传输端口无响应");
                }));
            }
            this.Dispatcher.Invoke(new Action(() =>
            {
                WriteLog("开始发送 " + new FileInfo(fi.FileName).Name);
                snackbar.MessageQueue.Enqueue("开始发送 " + new FileInfo(fi.FileName).Name);
            }));

            long offset = 0;

            this.Dispatcher.Invoke(new Action(() =>
            {
                progressBar_sendFile.Minimum = 0;
                progressBar_sendFile.Maximum = fi.FileLength;

                button_sendFile.IsEnabled = false;
            }));

            while (true)
            {
                //读取文件块
                byte[] Read_buffer = FileRead(fi.FileName, offset, File_Buffer_Size);
                int    length      = Read_buffer.Length;
                offset += length;

                //加密文件块
                byte[] Send_Buffer = DataCrypto.Encrypt(Read_buffer, AES);

                try
                {
                    length = socketSendFile.Send(Send_Buffer);
                }
                catch (Exception ex)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        WriteLog(ex.ToString());
                        WriteLog("传输连接已断开");

                        snackbar.MessageQueue.Enqueue("传输连接已断开,停止发送 " + new FileInfo(fi.FileName).Name);
                    }));
                    break;
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    progressBar_sendFile.Value = offset;
                }));

                if (offset >= fi.FileLength)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        WriteLog(new FileInfo(fi.FileName).Name + " 发送完毕");
                        snackbar.MessageQueue.Enqueue(new FileInfo(fi.FileName).Name + " 发送完毕");
                        socketSendFile.Shutdown(SocketShutdown.Both);
                        socketSendFile.Close();
                        socketSendFile = null;
                    }));
                    break;
                }

                System.GC.Collect();
            }

            this.Dispatcher.Invoke(new Action(() =>
            {
                button_sendFile.IsEnabled = true;
            }));
            System.GC.Collect();
        }