/// <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> /// 对包头文件进行解密 /// </summary> /// <param name="fileDate"></param> /// <param name="fs"></param> /// <returns></returns> internal static FileState FileHeadDecrypt(byte[] fileDate, FileStream fs) { int lable = ByteToDate.ByteToInt(3, fileDate); long fileLenth = ByteToDate.ByteToLong(7, fileDate); byte[] fileNameByte = new byte[fileDate.Length - 15]; Array.Copy(fileDate, 15, fileNameByte, 0, fileNameByte.Length); string fileName = Encoding.UTF8.GetString(fileNameByte); FileState haveState = new FileState(lable, fileLenth, fileName, fs); return(haveState); }
/// <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="textCode">数据模型的暗号</param> /// <param name="state">StateBase</param> /// <returns>加密之后的数据(1暗号+1暗号+4数据标签+date)</returns> private static byte[] encryptionTemporary(byte[] date, byte textCode, TransmitData state) { if (date.Length > state.BufferSize - 20) { //超出通过文件大数据包处理发送 return(EncDecSeparateDate.SendHeadEncryption(date, textCode, state)); } //给发送的数据进行编号 state.SendDateLabel = RandomPublic.RandomNumber(16787); //编号并加密 (加密 byte[] dateOverall = ByteToDate.OffsetEncryption(date, state.SendDateLabel, 2); dateOverall[0] = CipherCode._commonCode; dateOverall[1] = textCode; return(dateOverall); }
/// <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="stateOne">StateBase</param> /// <param name="insert">数据实际长度</param> /// <returns>需要的数据</returns> internal static byte[] DateOneManage(TransmitData stateOne, int insert) { byte[] receiveByte = null; if (stateOne.Buffer[0] == 0 && stateOne.BufferBackup != null && stateOne.BufferBackup.Length >= insert) { receiveByte = stateOne.BufferBackup; //清空备用缓冲区 stateOne.BufferBackup = null; }//主要用于缓冲区有扩大缩小 else { receiveByte = stateOne.Buffer; } byte[] haveDate = ByteToDate.ByteToByte(receiveByte, insert, 0); Array.Clear(stateOne.Buffer, 0, stateOne.Buffer.Length); return(haveDate); }
/// <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); }
/// <summary> /// 解密数组 /// </summary> /// <param name="receiveDate">接收到的数据</param> /// <param name="residualpackage">残留的数据</param> /// <returns>返加一个完整包的数据 (这里得到的是已去除粘包代码与数据长度的数据包)</returns> private static byte[] DecryptByte(byte[] receiveDate, ref byte[] residualpackage) { byte[] haveDate = null; // bool ddd = false; //取得粘包代码,(取前4字节) int stickPackageCode = ByteToDate.ByteToInt(0, receiveDate); if (stickPackageCode == CipherCode._stickPackageCode) { //为粘包代码时 if (receiveDate.Length < 9) //这里表示收到空的数据 { residualpackage = receiveDate; return(null); } //取数据长度 int datelenth = ByteToDate.ByteToInt(4, receiveDate); //清空残留数据 residualpackage = null; //对残留数据进行初始化 if (datelenth == receiveDate.Length - 8) { //说明整个收到的数据就是一个完整包 haveDate = new byte[datelenth]; Array.Copy(receiveDate, 8, haveDate, 0, datelenth); } else if (datelenth > receiveDate.Length - 8) { //说明数据没有收完全,把数据放在残留包里进行下一轮接收 residualpackage = receiveDate; } else { //说明有至少二个包粘在一起 //取出粘包的前面一个包数据 haveDate = new byte[datelenth]; //残留的数据长度 var residualDataLength = receiveDate.Length - 8 - datelenth; Array.Copy(receiveDate, 8, haveDate, 0, datelenth); //包后面的数据放到残留,把剩下的扔然放在残留里 residualpackage = new byte[residualDataLength]; Array.Copy(receiveDate, 8 + datelenth, residualpackage, 0, residualDataLength); } } else { //不为粘包代码时;说明数据有可能是前面一个接下去的 // ddd = true; if (residualpackage != null) { // 这里为上次 “说明数据没有收完全,把数据放在残留包里进行下一轮接收”这里下来的 //残留数据不为空,合并到接收的数据中(格式:残留数据+收到的数据) ,并清空残留 byte[] addDate = new byte[receiveDate.Length + residualpackage.Length]; residualpackage.CopyTo(addDate, 0); receiveDate.CopyTo(addDate, residualpackage.Length); receiveDate = addDate; residualpackage = null; } else//不知道是什么数据,直接扔掉 { return(null); } } return(haveDate); }