private void button_genRandom_Click(object sender, RoutedEventArgs e) { //连接可用性判断 if (socketConn == null || !socketConn.Connected) { this.Dispatcher.Invoke(new Action(() => { snackbar.MessageQueue.Enqueue("无可用连接"); })); return; } //如果已经认证通过就不需要再验证了 if (IsAuthenticated) { this.Dispatcher.Invoke(new Action(() => { snackbar.MessageQueue.Enqueue("已认证通过,无需再次操作"); })); return; } //生成一个100000-999999随机数作为挑战值challenge_value //要求对方收到挑战值后,对称密钥加密回送应答 int challenge_value = new Random().Next(100000, 999999); textBox_challengeValue.Text = challenge_value.ToString(); StringBuilder sBuilder = new StringBuilder(); byte[] hash; string Key; using (MD5 md5Hash = MD5.Create()) { //生成密钥 //Hash((1-challenge_value).ToString())作为预定密钥 hash = md5Hash.ComputeHash(Encoding.UTF8.GetBytes((1 - challenge_value).ToString())); foreach (byte b in hash) { sBuilder.Append(b.ToString("x2")); } Key = sBuilder.ToString(); textBox_key.Text = sBuilder.ToString(); //计算响应值 //Hash(Hash(challenge_value.ToString()))为应答 hash = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(challenge_value.ToString())); hash = md5Hash.ComputeHash(hash); } //计算响应密文(期望响应) AES = DataCrypto.GenAesCryptoServiceProvider(Key); //字符串采用base64编码 ExpertResponse = Convert.ToBase64String(DataCrypto.Encrypt(hash, AES)); textBox_expertResponse.Text = ExpertResponse; Authen_Message am = new Authen_Message(Status_Flag.Start_Challenge, DateTime.Now, challenge_value.ToString()); socketConn.Send(Message2Byte(am)); WriteLog("发起挑战,值为" + challenge_value.ToString()); WriteLog("生成预共享密钥"); snackbar.MessageQueue.Enqueue("发起挑战"); }
//将TcP包的内容转换为byte public static byte[] Message2Byte(Authen_Message am) { return(ByteConvertHelper.Object2Bytes(am)); }
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("连接已断开"); ResetAuthenInfo(); //重新开始监听 Thread threadWatch = new Thread(WatchingConn); threadWatch.IsBackground = true; threadWatch.Start(); })); break; } if (length <= 0) { continue; } 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); socketConn.Send(Message2Byte(au)); WriteLog("接收到超时的命令"); } if (am.Flag == Status_Flag.Response_Challenge) { this.Dispatcher.Invoke(new Action(() => { textBox_recRespomse.Text = am.Extend; WriteLog("收到回应" + am.Extend); snackbar.MessageQueue.Enqueue("收到回应"); })); if (ExpertResponse == am.Extend) { Authen_Message au = new Authen_Message(Status_Flag.Authen_Success, DateTime.Now, null); socketConn.Send(Message2Byte(au)); IsAuthenticated = true; this.Dispatcher.Invoke(new Action(() => { WriteLog("用户认证成功,密钥协商完毕,发送就绪"); snackbar.MessageQueue.Enqueue("用户认证成功"); })); Thread trd = new Thread(TransControl); trd.IsBackground = true; trd.Start(); break; } else { Authen_Message au = new Authen_Message(Status_Flag.Authen_Failed, DateTime.Now, null); socketConn.Send(Message2Byte(au)); this.Dispatcher.Invoke(new Action(() => { WriteLog("认证失败,您可以选择重新认证"); snackbar.MessageQueue.Enqueue("用户认证失败"); })); } } } }