/// <summary> /// 对文本和图片数据进行解密; /// </summary> /// <param name="date">接收到的数据</param> /// <param name="state">StateBase</param> /// <returns>返回一个图片</returns> internal static DataModel deciphering(byte[] date, TransmitData state) { DataModel stateCode = null; //小于6,是说:暗号类型1位+暗号1位+数据标签4位 if (date.Length < 6) { return(stateCode);//收到的数据不正确 } byte headDate = date[1]; //当是图片与文本时 if (headDate == CipherCode._textCode || headDate == CipherCode._photographCode) { int SendDateLabel = 0; //取得数据,并取得数据标签 byte[] dateAll = ByteToDate.OffsetDecrypt(date, out SendDateLabel, 2); //回复此数据标签的数据是成功收到了 byte[] ReplyDate = ByteToDate.CombinationTwo(CipherCode._commonCode, CipherCode._dateSuccess, SendDateLabel); //判断是否文本 if (headDate == CipherCode._textCode) { //文本 string str = Encoding.UTF8.GetString(dateAll); stateCode = new DataModel(CipherCode._textCode, str, ReplyDate);//解析出来是文本数据 } else { //图片 stateCode = new DataModel(CipherCode._photographCode, dateAll, ReplyDate);//解释出来是图片数据 } } else if (headDate == CipherCode._dateSuccess)//数据成功或重发 { //找出已发数据的标签 int SendDateLabel = ByteToDate.ByteToInt(2, date); if (headDate == CipherCode._dateSuccess) { stateCode = new DataModel(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 DataModel FileDecrypt(byte[] date, TransmitData state) { DataModel stateCode = null; if (date.Length < 6) { return(stateCode); } byte headDate = date[1]; if (headDate == CipherCode._fileAgreeReceive) {//对方同意接收文件;我应该怎么处理 int FileLabel = ByteToDate.ByteToInt(2, date); if (state.SendFile != null && state.SendFile.FileLabel == FileLabel) { byte[] SendSubjectDate = FileGetSendDate(state); if (SendSubjectDate == null) { stateCode = new DataModel(CipherCode._dateSuccess); } else { stateCode = new DataModel(SendSubjectDate);//直接发送 } } } else if (headDate == CipherCode._dateSuccess) {//对方已经接收到数据 int FileLabel = ByteToDate.ByteToInt(2, date); if (state.SendFile != null && state.SendFile.FileLabel == FileLabel) { byte[] SendSubjectDate = FileGetSendDate(state); if (SendSubjectDate == null) { stateCode = new DataModel(CipherCode._dateSuccess); } else { stateCode = new DataModel(SendSubjectDate);//直接发送 } } } //上面是发送方接收要做的;下面是接收方发送要做的事情 else if (headDate == CipherCode._fileHeadCode) { //收到的是文件包头部分 byte whatCode = date[2]; //原暗号 int fileLabel = ByteToDate.ByteToInt(3, date); //数据标签 int fileLenth = ByteToDate.ByteToInt(7, date); //长度 state.ReceiveFile = new TransmitFile(whatCode, fileLabel, fileLenth); byte[] dateAll = new byte[6]; dateAll[0] = CipherCode._bigDateCode; dateAll[1] = CipherCode._fileAgreeReceive; ByteToDate.IntToByte(fileLabel, 2, dateAll); stateCode = new DataModel(dateAll); } else if (headDate == CipherCode._fileSubjectCode) {//收到的是文件主体部分 int SendDateLabel = 0; byte[] dateAll = ByteToDate.OffsetDecrypt(date, out SendDateLabel, 2); byte[] ReplyDate = ByteToDate.CombinationTwo(CipherCode._bigDateCode, CipherCode._dateSuccess, state.ReceiveFile.FileLabel); if (state.ReceiveFile.FileDateAll == null) { state.ReceiveFile.FileDateAll = dateAll;//是第一次接收到主体数据 stateCode = new DataModel(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 == CipherCode._textCode) { string str = Encoding.UTF8.GetString(FileDateAll); stateCode = new DataModel(CipherCode._textCode, str, ReplyDate); } else { stateCode = new DataModel(CipherCode._photographCode, FileDateAll, ReplyDate); } state.ReceiveFile = null;//文件接收完成;释放接收器 } else { stateCode = new DataModel(ReplyDate); } } } return(stateCode); }