/// <summary> /// 对7位数的普通通信进行加密; /// </summary> /// <param name="whoDate">接收方还是发送方的数据</param> /// <param name="code">暗号</param> /// <param name="fileLabel">文件标签</param> /// <returns>加密完成的数据</returns> internal static byte[] FileSevenEncryption(byte whoDate, byte code, int fileLabel) { byte[] haveDate = new byte[7]; haveDate[0] = CipherCode._fileCode; haveDate[1] = whoDate; haveDate[2] = code; ByteToDate.IntToByte(fileLabel, 3, haveDate); return(haveDate); }
/// <summary> /// 对TCP发送进行粘包加密 /// </summary> /// <param name="sendDate">要加密的数据</param> /// <returns>加密之后的数据为:粘包代码4byte+数据包长度4byte</returns> /// <remarks>把数据包处理为:粘包代码4byte+数据包长度4byte</remarks> internal static void EncryptionPackage(ref byte[] sendDate) { //把数据包处理为:粘包代码4byte+数据包长度4byte byte[] dateAll = new byte[sendDate.Length + 8]; ByteToDate.IntToByte(CipherCode._stickPackageCode, 0, dateAll); ByteToDate.IntToByte(sendDate.Length, 4, dateAll); sendDate.CopyTo(dateAll, 8); sendDate = dateAll; }
/// <summary> /// 用于接收方;对续传时回复长度的数据进行加密 /// </summary> /// <param name="code">是同意时还是发起方</param> /// <param name="fileState">FileState</param> /// <returns>加密之后的数据</returns> internal static byte[] ReceiveContingueEncryption(byte code, FileState fileState) { byte[] haveDate = new byte[15]; haveDate[0] = CipherCode._fileCode; haveDate[1] = CipherCode._receiveUser; haveDate[2] = code; ByteToDate.IntToByte(fileState.FileLabel, 3, haveDate); ByteToDate.IntToByte(fileState.FileOkLenth, 7, haveDate); return(haveDate); }
/// <summary> /// 发送方对一个文件包头进行加密得到一个byte[] /// </summary> /// <param name="fileSend"></param> /// <returns></returns> internal static byte[] FileHeadEncryption(FileState fileSend) { string fileName = CommonMethod.StringRight(fileSend.FileName, "\\"); byte[] fileNameByte = Encoding.UTF8.GetBytes(fileName); byte[] haveDate = new byte[15 + fileNameByte.Length]; haveDate[0] = CipherCode._fileCode; haveDate[1] = CipherCode._sendUser; haveDate[2] = CipherCode._fileHeadCode; ByteToDate.IntToByte(fileSend.FileLabel, 3, haveDate); ByteToDate.IntToByte(fileSend.FileLenth, 7, haveDate); fileNameByte.CopyTo(haveDate, 15); return(haveDate); }
/// <summary> /// 返回分包数据11位的数据包包头(暗号类型1+暗号1+原暗号+数据标签+长度) /// </summary> /// <param name="date">数据</param> /// <param name="textCode">原暗号,什么文件</param> /// <param name="state">StateBase</param> /// <returns>加密之后的包头</returns> internal static byte[] SendHeadEncryption(byte[] date, byte textCode, TransmitData state) { state.SendFile = new TransmitFile(date); state.SendFile.FileLabel = RandomPublic.RandomNumber(14562); byte[] headDate = new byte[11]; //写入暗号 headDate[0] = CipherCode._bigDateCode; headDate[1] = CipherCode._fileHeadCode; headDate[2] = textCode; //写入数据标签 ByteToDate.IntToByte(state.SendFile.FileLabel, 3, headDate); //写入数据长度 ByteToDate.IntToByte(date.Length, 7, headDate); return(headDate); }
/// <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); }