/// <summary> /// 对文本和图片数据进行解密; /// </summary> /// <param name="date">接收到的数据</param> /// <param name="state">StateBase</param> /// <returns>StateCode</returns> internal static StateCode deciphering(byte[] date, StateBase state) { StateCode stateCode = null; if (date.Length < 6) { return(stateCode); //收到的数据不正确 } byte headDate = date[1]; if (headDate == PasswordCode._textCode || headDate == PasswordCode._photographCode) //解密到的是文本数据 { int SendDateLabel = 0; byte[] dateAll = ByteToDate.OffsetDecrypt(date, out SendDateLabel, 2); byte[] ReplyDate = ByteToDate.CombinationTwo(PasswordCode._commonCode, PasswordCode._dateSuccess, SendDateLabel); //直接回复发送成功 if (headDate == PasswordCode._textCode) { string str = Encoding.UTF8.GetString(dateAll); stateCode = new StateCode(PasswordCode._textCode, str, ReplyDate); //解析出来是文本数据 } else { stateCode = new StateCode(PasswordCode._photographCode, dateAll, ReplyDate); //解释出来是图片数据 } } else if (headDate == PasswordCode._dateSuccess) //数据成功或重发 { int SendDateLabel = ByteToDate.ByteToInt(2, date); //找出已发数据的标签 if (headDate == PasswordCode._dateSuccess) { stateCode = new StateCode(headDate); if (SendDateLabel == state.SendDateLabel) { state.SendDate = null; } //已经成功对已发数据进行删除 } } return(stateCode); }
/// <summary> /// 当收到是分组数据代码的到这里来统一处理 /// </summary> /// <param name="date">数据</param> /// <param name="state">StateBase</param> /// <returns>StateCode</returns> internal static StateCode FileDecrypt(byte[] date, StateBase state) { StateCode stateCode = null; if (date.Length < 6) { return(stateCode); } byte headDate = date[1]; if (headDate == PasswordCode._fileAgreeReceive) {//对方同意接收文件;我应该怎么处理 int FileLabel = ByteToDate.ByteToInt(2, date); if (state.SendFile != null && state.SendFile.FileLabel == FileLabel) { byte[] SendSubjectDate = FileGetSendDate(state); if (SendSubjectDate == null) { stateCode = new StateCode(PasswordCode._dateSuccess); } else { stateCode = new StateCode(SendSubjectDate);//直接发送 } } } else if (headDate == PasswordCode._dateSuccess) {//对方已经接收到数据 int FileLabel = ByteToDate.ByteToInt(2, date); if (state.SendFile != null && state.SendFile.FileLabel == FileLabel) { byte[] SendSubjectDate = FileGetSendDate(state); if (SendSubjectDate == null) { stateCode = new StateCode(PasswordCode._dateSuccess); } else { stateCode = new StateCode(SendSubjectDate);//直接发送 } } } //上面是发送方接收要做的;下面是接收方发送要做的事情 else if (headDate == PasswordCode._fileHeadCode) {//收到的是文件包头部分 byte whatCode = date[2]; int fileLabel = ByteToDate.ByteToInt(3, date); int fileLenth = ByteToDate.ByteToInt(7, date); state.ReceiveFile = new FileBase(whatCode, fileLabel, fileLenth); byte[] dateAll = new byte[6]; dateAll[0] = PasswordCode._bigDateCode; dateAll[1] = PasswordCode._fileAgreeReceive; ByteToDate.IntToByte(fileLabel, 2, dateAll); stateCode = new StateCode(dateAll); } else if (headDate == PasswordCode._fileSubjectCode) {//收到的是文件主体部分 int SendDateLabel = 0; byte[] dateAll = ByteToDate.OffsetDecrypt(date, out SendDateLabel, 2); byte[] ReplyDate = ByteToDate.CombinationTwo(PasswordCode._bigDateCode, PasswordCode._dateSuccess, state.ReceiveFile.FileLabel); if (state.ReceiveFile.FileDateAll == null) { state.ReceiveFile.FileDateAll = dateAll;//是第一次接收到主体数据 stateCode = new StateCode(ReplyDate); } else { byte[] FileDateAll = new byte[state.ReceiveFile.FileDateAll.Length + dateAll.Length]; state.ReceiveFile.FileDateAll.CopyTo(FileDateAll, 0); dateAll.CopyTo(FileDateAll, state.ReceiveFile.FileDateAll.Length); state.ReceiveFile.FileDateAll = FileDateAll; if (FileDateAll.Length == state.ReceiveFile.FileLenth) { if (state.ReceiveFile.FileClassification == PasswordCode._textCode) { string str = Encoding.UTF8.GetString(FileDateAll); stateCode = new StateCode(PasswordCode._textCode, str, ReplyDate); } else { stateCode = new StateCode(PasswordCode._photographCode, FileDateAll, ReplyDate); } state.ReceiveFile = null;//文件接收完成;释放接收器 } else { stateCode = new StateCode(ReplyDate); } } } return(stateCode); }