/// <summary> /// 发送文件 /// </summary> /// <param name="param">续传断点</param> private void SendFile(object param) { long breakpoint = Convert.ToInt64(param); string srcFile = InvokeCtrl.GetCtrlText(txtFile); string hashcode = FileUtils.GetHashCode(srcFile); string filename = FileUtils.GetFileName(srcFile); totalBytes = FileUtils.GetFileSize(srcFile); if (totalBytes - breakpoint <= 0) { return; } //间歇时间 Thread.Sleep(interval); MsgEntity msg = new MsgEntity(); msg.msgType = Constant.MSG_TYPE_CMD; msg.msgBody = Constant.CMD_FILE_TRANSFER_REQ; msg.args = new Dictionary <string, object>(); msg.args.Add("filename", filename); //文件名 msg.args.Add("hashcode", hashcode); //文件hash值 msg.args.Add("total", totalBytes); //文件大小 msg.args.Add("reseed", (breakpoint > 0)); //是否为续传 msg.args.Add("sendstate", ""); //传输状态(发送中/发送结束) msg.args.Add("offset", 0); //传输流的偏移量 int size = buffersize * 1024; if (breakpoint + size > totalBytes) { msg.args["sendstate"] = Constant.STATE_SEND_END; timerSendState.Stop(); } else { msg.args["sendstate"] = Constant.STATE_SEND_ON; } msg.args["offset"] = breakpoint; byte[] extendData = FileUtils.ReadFromFile(srcFile, size, breakpoint); byte[] sendbytes = FileUtils.GetSendBytes(msg, extendData); SendByteData(sendbytes); if (chkShowLog.Checked) { Log("发送文件流len=" + sendbytes.Length); } completeBytes += extendData.Length; int finish = (int)Math.Ceiling(100.0 * completeBytes / totalBytes); InvokeCtrl.UpdatePrgBarValue(prgBar1, finish, Color.FromKnownColor(KnownColor.Highlight), string.Format("发送完成{0} %", finish)); InvokeCtrl.UpdateCtrlText(lbFinish, FileUtils.getFinished(completeBytes, totalBytes)); }
private void updateState() { milliseconds += 100; lbSeconds.Text = getUseTime(milliseconds); InvokeCtrl.UpdatePrgBarValue(prgBar1, prgValue, Color.FromKnownColor(KnownColor.Highlight), string.Format("{0}%", prgValue / 100)); InvokeCtrl.UpdateCtrlText(lbCounter, counter.ToString()); InvokeCtrl.UpdateCtrlText(txtDesKey, desKey); InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen); }
//按钮事件:发送文件 private void btnSendFile_Click(object sender, EventArgs e) { if (txtFile.Text == string.Empty) { MessageBox.Show("请选择要传输的文件", "提示"); return; } if (!File.Exists(txtFile.Text)) { MessageBox.Show("要传输的文件不存在", "提示"); return; } btnSendFile.Enabled = false; btnPause.Enabled = true; if (btnSendFile.Text == "续传文件") { string hashcode = FileUtils.GetHashCode(txtFile.Text); //发送检索文件断点请求 MsgEntity msg = new MsgEntity(Constant.MSG_TYPE_CMD, Constant.CMD_FILE_BREAKPOINT_REQ); msg.args = new Dictionary <string, object>(); msg.args.Add("hashcode", hashcode); byte[] sendbytes = FileUtils.GetSendBytes(msg); SendByteData(sendbytes); } else { sendThread = new Thread(SendFile); sendThread.Start(0); timerSendState.Start(); InvokeCtrl.UpdateCtrlEnabled(btnPause, true); milliseconds = 0; finished = false; countBytes = completeBytes = 0; } }
private void crackDES() { do { if (counter > total) { timerCrackState.Stop(); InvokeCtrl.UpdateCtrlText(txtDesKey, desKey); InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen); MessageBox.Show("任务结束,未找到密钥!"); InvokeCtrl.UpdateCtrlEnabled(btnCrack, true); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); InvokeCtrl.UpdateCtrlEnabled(btnStop, false); InvokeCtrl.UpdateCtrlEnabled(txtPlaintext, true); InvokeCtrl.UpdateCtrlEnabled(txtCiphertext, true); InvokeCtrl.UpdateCtrlEnabled(txtThreadNum, true); LogHelper.Log(string.Format("任务结束,用时[{0}],未找到密钥!", InvokeCtrl.GetCtrlText(lbSeconds))); break; } /* * 1、假设密钥由md5生成(即字符范围[0-9A-F]),那么8位的密钥范围可以看作0x00000000-0xFFFFFFFF * 2、假设md5生成字符统一大写或小写,那么需要考虑切换大小写再加密 * 2、假设加密向量与密钥相同(否则计算量超出本算法范围。。) */ desKey = counter.ToString("x8"); counter++; prgValue = Convert.ToInt32(10000 * counter / total); //InvokeCtrl.UpdateCtrlText(lbCounter, counter.ToString()); //InvokeCtrl.UpdateCtrlText(txtDesKey, desKey); cipherGen = DESCrypt.Encrypt(desKey, plaintext, desKey); //InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen); if (cipherGen.Equals(ciphertext)) { timerCrackState.Stop(); InvokeCtrl.UpdateCtrlText(txtDesKey, desKey); InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen); MessageBox.Show("恭喜你,找到密钥:" + cipherGen); InvokeCtrl.UpdateCtrlEnabled(btnCrack, true); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); InvokeCtrl.UpdateCtrlEnabled(btnStop, false); InvokeCtrl.UpdateCtrlEnabled(txtPlaintext, true); InvokeCtrl.UpdateCtrlEnabled(txtCiphertext, true); InvokeCtrl.UpdateCtrlEnabled(txtThreadNum, true); LogHelper.Log(string.Format("在进度[{0}]处,用时[{1}],找到密钥:{2}", InvokeCtrl.GetCtrlText(prgBar1), InvokeCtrl.GetCtrlText(lbSeconds), cipherGen)); break; } string desKeyUpcase = desKey.ToUpper(); if (desKeyUpcase.Equals(desKey)) { continue; } desKey = desKeyUpcase; //InvokeCtrl.UpdateCtrlText(txtDesKey, desKey); cipherGen = DESCrypt.Encrypt(desKey, plaintext, desKey); //InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen); if (cipherGen.Equals(ciphertext)) { timerCrackState.Stop(); InvokeCtrl.UpdateCtrlText(txtDesKey, desKey); InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen); MessageBox.Show("恭喜你,找到密钥:" + cipherGen); InvokeCtrl.UpdateCtrlEnabled(btnCrack, true); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); InvokeCtrl.UpdateCtrlEnabled(btnStop, false); InvokeCtrl.UpdateCtrlEnabled(txtPlaintext, true); InvokeCtrl.UpdateCtrlEnabled(txtCiphertext, true); InvokeCtrl.UpdateCtrlEnabled(txtThreadNum, true); LogHelper.Log(string.Format("在进度[{0}]处,用时[{1}],找到密钥:{2}", InvokeCtrl.GetCtrlText(prgBar1), InvokeCtrl.GetCtrlText(lbSeconds), cipherGen)); break; } } while (true); }
// 接收消息方法 private void ReceiveMessage() { if (rdBtnTCP.Checked) { receiveUpdClient.AcceptNew(); } while (true) { try { // 关闭receiveUdpClient时此时会产生异常 byte[] receiveBytes = receiveUpdClient.Receive(ref remoteIpEndPoint); if (receiveBytes.Length <= 0) { continue; } byte[] sendbytes; byte[] extendData = new byte[0]; MsgEntity msg = (MsgEntity)FileUtils.GetMsgEntity(receiveBytes, ref extendData); if (msg.msgType == Constant.MSG_TYPE_TXT)//收到文本消息 { string message = msg.msgBody; // 显示消息内容 Log(string.Format("{0}[{1}]", remoteIpEndPoint, message)); //响应收到 MsgEntity msg2 = new MsgEntity(); msg2.msgType = Constant.MSG_TYPE_CMD; msg2.msgBody = Constant.CMD_RECEIVE_RSP; msg2.args = new Dictionary <string, object>(); msg2.args.Add("rspText", "收到"); sendbytes = FileUtils.GetSendBytes(msg2); SendByteData(sendbytes); } else if (msg.msgType == Constant.MSG_TYPE_CMD) //收到命令消息 { if (msg.msgBody == Constant.SESSION_CODE_EXIT) //收到退出指令 { Log("客户端主动断开:" + remoteIpEndPoint); receiveUpdClient.AcceptNew(); InvokeCtrl.UpdateCtrlEnabled(btnSendFile, false); InvokeCtrl.UpdateCtrlEnabled(btnSendMsg, false); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); InvokeCtrl.UpdateCtrlEnabled(rdBtnUDP, true); InvokeCtrl.UpdateCtrlEnabled(rdBtnTCP, true); continue; } else if (msg.msgBody == Constant.SESSION_CODE_LOGIN)//收到登录指令 { Log("客户端连接:" + remoteIpEndPoint); InvokeCtrl.UpdateCtrlEnabled(btnSendFile, true); InvokeCtrl.UpdateCtrlEnabled(btnSendMsg, true); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); InvokeCtrl.UpdateCtrlEnabled(rdBtnUDP, false); InvokeCtrl.UpdateCtrlEnabled(rdBtnTCP, false); continue; } else if (msg.msgBody == Constant.CMD_RECEIVE_RSP) //响应已接收 { if (chkShowLog.Checked) { // 显示响应内容 string rspText = (string)msg.args["rspText"]; Log(string.Format("{0}[{1}]", remoteIpEndPoint, rspText)); } } else if (msg.msgBody == Constant.CMD_FILE_BREAKPOINT_REQ)//收到检索文件断点请求 { MsgEntity msg2 = new MsgEntity(); msg2.msgType = Constant.MSG_TYPE_CMD; msg2.msgBody = Constant.CMD_FILE_BREAKPOINT_RSP;//响应请求 msg2.args = new Dictionary <string, object>(); msg2.args.Add("fileexits", false); string hashcode = (string)msg.args["hashcode"]; FileInfoEntity entity = FileUtils.getFileInfo(sendFileInfo, hashcode); if (entity != null) { string destFile = entity.filepath + entity.filename; if (File.Exists(destFile)) { long filesize = FileUtils.GetFileSize(destFile); msg2.args["fileexits"] = true; msg2.args.Add("breakpoint", filesize); } } sendbytes = FileUtils.GetSendBytes(msg2); SendByteData(sendbytes); continue; } else if (msg.msgBody == Constant.CMD_FILE_TRANSFER_REQ)//收到文件传输请求 { if (chkShowLog.Checked) { Log("收到文件流len=" + (receiveBytes.Length + 4) + "来自:" + remoteIpEndPoint); } //响应收到 MsgEntity msg2 = new MsgEntity(); msg2.msgType = Constant.MSG_TYPE_CMD; msg2.msgBody = Constant.CMD_RECEIVE_RSP; msg2.args = new Dictionary <string, object>(); msg2.args.Add("rspText", "收到"); sendbytes = FileUtils.GetSendBytes(msg2); SendByteData(sendbytes); string filename = (string)msg.args["filename"]; string hashcode = (string)msg.args["hashcode"]; string sendstate = (string)msg.args["sendstate"]; long total = (long)msg.args["total"]; bool reseed = (bool)msg.args["reseed"]; long offset = (long)msg.args["offset"]; string folder = txtFolder.Text.Trim(); if (folder == "") { folder = AppDomain.CurrentDomain.BaseDirectory; } folder = folder.TrimEnd('\\') + "\\"; FileInfoEntity fileInfoEntity = FileUtils.getFileInfo(sendFileInfo, hashcode); if (fileInfoEntity == null) { fileInfoEntity = new FileInfoEntity(hashcode, folder, filename); sendFileInfo.fileList.Add(fileInfoEntity); } //非续传,且非发送中状态,更新文件路径 if (!reseed && fileInfoEntity.filestate != Constant.STATE_FILE_SENDING) { fileInfoEntity.filepath = folder; fileInfoEntity.filename = filename; } string destFile = fileInfoEntity.filepath + fileInfoEntity.filename; if (File.Exists(destFile)) { //文件存在,非续传,且非发送中状态,则覆盖 if (!reseed && fileInfoEntity.filestate != Constant.STATE_FILE_SENDING) { File.Delete(destFile); } } else { if (reseed)//文件不存在,续传则取消 { SendMessage("要续传的文件已丢失"); continue; } } try { if (sendstate == Constant.STATE_SEND_ON) { FileUtils.WriteToFile(destFile, extendData, offset); fileInfoEntity.offset = offset + extendData.Length; fileInfoEntity.filestate = Constant.STATE_FILE_SENDING; MsgEntity msg3 = new MsgEntity(); msg3.msgType = Constant.MSG_TYPE_CMD; msg3.msgBody = Constant.CMD_FILE_CONTINUE_RSP;//响应传输继续 msg3.args = new Dictionary <string, object>(); msg3.args.Add("breakpoint", fileInfoEntity.offset); sendbytes = FileUtils.GetSendBytes(msg3); SendByteData(sendbytes); } else if (sendstate == Constant.STATE_SEND_END) { FileUtils.WriteToFile(destFile, extendData, offset); fileInfoEntity.offset = offset + extendData.Length; //进行文件校验 string filecode = FileUtils.GetHashCode(destFile); if (filecode.Equals(hashcode)) { fileInfoEntity.filestate = Constant.STATE_FILE_FINISH; //发送接收完成信息 msg = new MsgEntity(Constant.MSG_TYPE_CMD, Constant.CMD_FILE_FINISH_RSP); sendbytes = FileUtils.GetSendBytes(msg); SendByteData(sendbytes); Log("文件校验成功!"); Log("已保存路径为:" + destFile); } else { fileInfoEntity.filestate = Constant.STATE_FILE_FAILD; //发送接收失败信息 msg = new MsgEntity(Constant.MSG_TYPE_CMD, Constant.CMD_FILE_FAILD_RSP); sendbytes = FileUtils.GetSendBytes(msg); SendByteData(sendbytes); Log("文件校验失败!"); Log("已保存路径为:" + destFile); } } } catch (Exception e) { fileInfoEntity.filestate = Constant.STATE_FILE_FAILD; //发送接收失败信息 msg = new MsgEntity(Constant.MSG_TYPE_CMD, Constant.CMD_FILE_FAILD_RSP); sendbytes = FileUtils.GetSendBytes(msg); SendByteData(sendbytes); Log("接收消息线程出错:" + e.Message); } //发送文件过程中,需要及时保存文件断点信息,以免程序异常退出 string path = AppDomain.CurrentDomain.BaseDirectory; string strSendInfo = path + Constant.FILENAME_SENDINFO; FileUtils.SerializeFile(strSendInfo, sendFileInfo); int finish = (int)Math.Ceiling(100.0 * fileInfoEntity.offset / total); InvokeCtrl.UpdatePrgBarValue(prgBar1, finish, Color.OrangeRed, string.Format("接收完成{0} %", finish)); InvokeCtrl.UpdateCtrlText(lbFinish, FileUtils.getFinished(fileInfoEntity.offset, total)); } else if (msg.msgBody == Constant.CMD_FILE_CONTINUE_RSP)//收到继续传输的响应 { long breakpoint = (long)msg.args["breakpoint"]; sendThread = new Thread(SendFile); sendThread.Start(breakpoint); } else if (msg.msgBody == Constant.CMD_FILE_FINISH_RSP)//收到文件传输成功响应 { InvokeCtrl.UpdateCtrlText(btnSendFile, "发送文件"); InvokeCtrl.UpdateCtrlEnabled(btnSendFile, true); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); finished = true; timerSendState.Stop(); Log(string.Format("{0}[{1}]", remoteIpEndPoint, "文件接收完毕")); } else if (msg.msgBody == Constant.CMD_FILE_FAILD_RSP)//收到文件传输失败响应 { InvokeCtrl.UpdateCtrlEnabled(btnSendFile, true); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); finished = false; timerSendState.Stop(); Log(string.Format("{0}[{1}]", remoteIpEndPoint, "文件接收失败")); } else if (msg.msgBody == Constant.CMD_FILE_BREAKPOINT_RSP)//收到检索文件断点响应 { bool fileexits = (bool)msg.args["fileexits"]; if (fileexits) { long breakpoint = (long)msg.args["breakpoint"]; sendThread = new Thread(SendFile); sendThread.Start(breakpoint); timerSendState.Start(); InvokeCtrl.UpdateCtrlEnabled(btnPause, true); milliseconds = 0; finished = false; countBytes = completeBytes = breakpoint; } else { DialogResult dlgResult = MessageBox.Show("对方文件不存在,是否重新上传?", "提示", MessageBoxButtons.YesNo); if (DialogResult.Yes == dlgResult) { sendThread = new Thread(SendFile); sendThread.Start(0); InvokeCtrl.UpdateCtrlText(btnSendFile, "发送文件"); timerSendState.Start(); InvokeCtrl.UpdateCtrlEnabled(btnPause, true); milliseconds = 0; finished = false; countBytes = completeBytes = 0; } else { InvokeCtrl.UpdateCtrlEnabled(btnSendFile, true); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); } } } } } catch (Exception e) { Log("接收消息线程出错:" + e.Message); InvokeCtrl.UpdateCtrlEnabled(btnSendFile, true); InvokeCtrl.UpdateCtrlEnabled(btnPause, false); timerSendState.Stop(); break; } } }