Пример #1
0
 /// <summary>
 /// 把一个端口号放到数据里加密
 /// </summary>
 /// <param name="port">端口号</param>
 /// <param name="date">数据</param>
 /// <returns>返回的数据</returns>
 internal static byte[] SetPort(int port, byte[] date)
 {
     byte[] haveDate = new byte[date.Length + 4];
     ByteToDate.IntToByte(port, 0, haveDate);
     date.CopyTo(haveDate, 4);
     return(haveDate);
 }
Пример #2
0
 /// <summary>
 /// 对TCP发送进行粘包加密;
 /// </summary>
 /// <param name="sendDate">要加密的数据</param>
 /// <returns>加密之后的数据</returns>
 internal static void EncryptionPackage(ref byte[] sendDate)
 {
     byte[] dateAll = new byte[sendDate.Length + 8];
     ByteToDate.IntToByte(PasswordCode._stickPackageCode, 0, dateAll);
     ByteToDate.IntToByte(sendDate.Length, 4, dateAll);
     sendDate.CopyTo(dateAll, 8);
     sendDate = dateAll;
 }
Пример #3
0
 /// <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] = PasswordCode._fileCode;
     haveDate[1] = whoDate;
     haveDate[2] = code;
     ByteToDate.IntToByte(fileLabel, 3, haveDate);
     return(haveDate);
 }
Пример #4
0
 /// <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] = PasswordCode._fileCode;
     haveDate[1] = PasswordCode._receiveUser;
     haveDate[2] = code;
     ByteToDate.IntToByte(fileState.FileLabel, 3, haveDate);
     ByteToDate.IntToByte(fileState.FileOkLenth, 7, haveDate);
     return(haveDate);
 }
 /// <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);
 }
Пример #6
0
        /// <summary>
        /// 发送方对一个文件包头进行加密得到一个byte[]
        /// </summary>
        /// <param name="fileSend"></param>
        /// <returns></returns>
        internal static byte[] FileHeadEncryption(FileState fileSend)
        {
            string fileName = EngineTool.StringRight(fileSend.FileName, "\\");

            byte[] fileNameByte = Encoding.UTF8.GetBytes(fileName);
            byte[] haveDate     = new byte[15 + fileNameByte.Length];
            haveDate[0] = PasswordCode._fileCode;
            haveDate[1] = PasswordCode._sendUser;
            haveDate[2] = PasswordCode._fileHeadCode;
            ByteToDate.IntToByte(fileSend.FileLabel, 3, haveDate);
            ByteToDate.IntToByte(fileSend.FileLenth, 7, haveDate);
            fileNameByte.CopyTo(haveDate, 15);
            return(haveDate);
        }
        /// <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);
        }