/// <summary> /// 发送文件初始化;发送文件前先发一个小包让对方进行确认 /// </summary> /// <param name="date">数据</param> /// <param name="textCode">什么文件</param> /// <param name="state">StateBase</param> /// <returns>加密之后的包头</returns> internal static byte[] SendHeadEncryption(byte[] date, byte textCode, StateBase state) { state.SendFile = new FileBase(date); state.SendFile.FileLabel = RandomPublic.RandomNumber(14562); byte[] headDate = new byte[11]; headDate[0] = PasswordCode._bigDateCode; headDate[1] = PasswordCode._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>加密之后的数据</returns> private static byte[] encryptionTemporary(byte[] date, byte textCode, StateBase state) { if (date.Length > state.BufferSize - 20) { return(EncryptionDecryptSeparateDate.SendHeadEncryption(date, textCode, state)); //超出通过文件系统发送 } state.SendDateLabel = RandomPublic.RandomNumber(16787); //给发送的数据进行编号 byte[] dateOverall = ByteToDate.OffsetEncryption(date, state.SendDateLabel, 2); dateOverall[0] = PasswordCode._commonCode; dateOverall[1] = textCode; return(dateOverall); }
/// <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> private void start() { if (reconnectOn)//如果是重连的延迟10秒 { Thread.Sleep(9000 + RandomPublic.RandomTime(1000)); } try { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.SendTimeout = 1000; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true); socket.BeginConnect(IpEndPoint, new AsyncCallback(AcceptCallback), socket); loginTimeout(socket);//超时判断方法 }catch (Exception Ex) { loginFailure(Ex.Message);//登录失败触发 } }
/// <summary> /// 发送文件 /// </summary> /// <param name="fileLable">文件标签</param> /// <param name="fileName">文件地址</param> /// <param name="stateOne">StateBase</param> /// <returns>形成之后的数据</returns> internal byte[] Send(ref int fileLable, string fileName, StateBase stateOne) { int fileLenth = 0; FileStream fs; try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); fileLenth = (int)fs.Length; } catch (Exception Ex) { throw new Exception(Ex.Message); } fileLable = RandomPublic.RandomNumber(16787); FileState fileState = new FileState(fileLable, fileLenth, fileName, fs); fileState.StateOne = stateOne; FS.Add(fileState); byte[] haveByte = EncryptionDecryptFile.FileHeadEncryption(fileState); return(haveByte); }