Exemple #1
0
        /// <summary>
        /// 取出一个端口号;同时得到去除这个端口号的数据;
        /// </summary>
        /// <param name="date">数据</param>
        /// <returns>返回端口号</returns>
        internal static int GetPort(ref byte[] date)
        {
            int haveInt = ByteToDate.ByteToInt(0, date);

            date = ByteToDate.ByteToByte(date, date.Length - 4, 4);
            return(haveInt);
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        private static byte[] DecryptByte(byte[] receiveDate, ref byte[] residualpackage)
        {
            byte[] haveDate         = null;
            bool   ddd              = false;
            int    stickPackageCode = ByteToDate.ByteToInt(0, receiveDate);

            if (stickPackageCode != PasswordCode._stickPackageCode)
            {//第一个与暗号不相同;说明数据有可能是前面一个接下去的
                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);
                }
            }
            if (ddd)
            {
                stickPackageCode = ByteToDate.ByteToInt(0, receiveDate);
            }
            if (stickPackageCode == PasswordCode._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];
                    Array.Copy(receiveDate, 8, haveDate, 0, datelenth);
                    residualpackage = new byte[receiveDate.Length - 8 - datelenth];
                    Array.Copy(receiveDate, 8 + datelenth, residualpackage, 0, receiveDate.Length - 8 - datelenth);
                    //把剩下的扔然放在残留里
                }
            }
            return(haveDate);
        }
Exemple #4
0
        /// <summary>
        /// 对文本和图片数据进行解密;
        /// </summary>
        /// <param name="date">接收到的数据</param>
        /// <param name="state">StateBase</param>
        /// <returns>StateCode</returns>
        internal static StateCode deciphering(byte[] date, StateBase state)
        {
            StateCode stateCode = null;

            if (date.Length < 6)
            {
                return(stateCode);//收到的数据不正确
            }
            byte headDate = date[1];

            if (headDate == PasswordCode._textCode || headDate == PasswordCode._photographCode)//解密到的是文本数据
            {
                int    SendDateLabel = 0;
                byte[] dateAll       = ByteToDate.OffsetDecrypt(date, out SendDateLabel, 2);
                byte[] ReplyDate     = ByteToDate.CombinationTwo(PasswordCode._commonCode, PasswordCode._dateSuccess, SendDateLabel);//直接回复发送成功
                if (headDate == PasswordCode._textCode)
                {
                    string str = Encoding.UTF8.GetString(dateAll);
                    stateCode = new StateCode(PasswordCode._textCode, str, ReplyDate);        //解析出来是文本数据
                }
                else
                {
                    stateCode = new StateCode(PasswordCode._photographCode, dateAll, ReplyDate);        //解释出来是图片数据
                }
            }
            else if (headDate == PasswordCode._dateSuccess)        //数据成功或重发
            {
                int SendDateLabel = ByteToDate.ByteToInt(2, date); //找出已发数据的标签
                if (headDate == PasswordCode._dateSuccess)
                {
                    stateCode = new StateCode(headDate);
                    if (SendDateLabel == state.SendDateLabel)
                    {
                        state.SendDate = null;
                    }                         //已经成功对已发数据进行删除
                }
            }
            return(stateCode);
        }
Exemple #5
0
        /// <summary>
        /// 接收方发过来的数据;
        /// </summary>
        /// <param name="receiveToDate">收到的数据</param>
        /// <param name="stateOne">StateBase</param>
        /// <returns>回复的数据</returns>
        internal byte[] ReceiveDateTO(byte[] receiveToDate, StateBase stateOne)
        {
            byte[]    haveDate  = null;
            int       fileLabel = ByteToDate.ByteToInt(3, receiveToDate);
            byte      code      = receiveToDate[2];
            FileState state     = FileLabelToState(fileLabel);

            if (state == null)
            {
                if (code == PasswordCode._fileCancel)
                {
                    return(null);
                }
                else
                {
                    return(EncryptionDecryptFile.FileSevenEncryption(PasswordCode._sendUser, PasswordCode._fileCancel, fileLabel));
                }
            }
            else
            {
                switch (code)
                {
                case PasswordCode._fileOk:   //对方同意接收文件
                    Thread.Sleep(500);
                    state.StateFile = 1;
                    haveDate        = EncryptionDecryptFile.FileSubjectEncryption(state, stateOne.BufferSize);
                    SendMust.FileStartOn(fileLabel);   //第一次发送用原来尺寸
                    break;

                case PasswordCode._fileRefuse:   //对方拒绝接收文件
                    FileRemove(fileLabel);
                    SendMust.FileRefuse(fileLabel);
                    break;

                case PasswordCode._dateSuccess:   //主体部分数据发送成功;准备发送下一批
                    haveDate = EncryptionDecryptFile.FileSubjectEncryption(state, BufferSize);
                    SendMust.FileProgress(state);
                    if (haveDate == null)//说明这个文件已经发送成功了
                    {
                        FileRemove(fileLabel); SendMust.SendSuccess(fileLabel);
                    }
                    break;

                case PasswordCode._fileCancel:   //对方已经取消了这个文件;
                    FileRemove(fileLabel);
                    SendMust.FileCancel(fileLabel);
                    break;

                case PasswordCode._sendStop:   //对方暂停发送
                    FileStopIn(state, SendMust);
                    break;

                case PasswordCode._fileContinue:                          //对方发过来一个续传的确认信息;你是否同意;
                    state.StateOne = stateOne;                            //有可能stateOne会有变化
                    FileStopIn(state, SendMust);                          //对方已经暂停了这个文件;我这边肯定也要先暂停掉
                    bool orStop = SendMust.FileOrNotContingue(fileLabel); //让客户确认;是否续传
                    if (orStop)
                    {
                        state.StateFile           = 1;
                        state.FileOkLenth         = ByteToDate.ByteToLong(7, receiveToDate);
                        state.Filestream.Position = state.FileOkLenth;   //设置流的当前读位置
                        haveDate = EncryptionDecryptFile.FileSubjectEncryption(state, stateOne.BufferSize);
                        if (haveDate != null)
                        {
                            haveDate[2] = PasswordCode._fileContinueOk;
                            SendMust.FileContinue(fileLabel);
                        }
                    }
                    else
                    {
                        haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._sendUser, PasswordCode._fileContinueNo, fileLabel);
                    }
                    break;

                case PasswordCode._fileContinueOk:   //对方同意续传
                    state.FileOkLenth         = ByteToDate.ByteToLong(7, receiveToDate);
                    state.StateFile           = 1;
                    state.Filestream.Position = state.FileOkLenth;       //设置流的当前读位置
                    haveDate = EncryptionDecryptFile.FileSubjectEncryption(state, stateOne.BufferSize);
                    SendMust.FileContinue(fileLabel);
                    break;

                case PasswordCode._fileContinueNo:   //对方拒绝续传
                    SendMust.FileNoContinue(fileLabel);
                    FileStopIn(state, SendMust);
                    break;
                }
            }
            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);
        }
Exemple #7
0
        /// <summary>
        /// 文件数据分析处理中心;如果是发送方发过来的数据让接收方去处理
        /// </summary>
        /// <param name="fileDate">文件数据</param>
        /// <param name="stateOne">StateBase</param>
        /// <returns>处理以后的数据</returns>
        internal static byte[] ReceiveDateTO(byte[] fileDate, StateBase stateOne)
        {
            if (fileDate.Length < 7)
            {
                return(null);
            }
            byte[] haveDate = null;
            byte   code     = fileDate[1];

            if (code == PasswordCode._receiveUser)
            {
                if (fileSend == null)//这里没有开启;直接让对方取消
                {
                    haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._sendUser, PasswordCode._fileCancel, ByteToDate.ByteToInt(3, fileDate));
                }
                else
                {
                    haveDate = fileSend.ReceiveDateTO(fileDate, stateOne);
                }
            }
            else if (code == PasswordCode._sendUser)
            {
                if (fileReceive == null)//这里没有开启;直接让对方取消
                {
                    haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileCancel, ByteToDate.ByteToInt(3, fileDate));
                }
                else
                {
                    haveDate = fileReceive.ReceiveDateTO(fileDate, stateOne);
                }
            }
            return(haveDate);
        }
Exemple #8
0
        /// <summary>
        /// 发送方发过来的数据;
        /// </summary>
        /// <param name="receiveToDate">收到的数据</param>
        /// <param name="stateOne">StateBase</param>
        /// <returns>需要回复的数据</returns>
        internal byte[] ReceiveDateTO(byte[] receiveToDate, StateBase stateOne)
        {
            byte[] haveDate  = null;
            int    fileLabel = ByteToDate.ByteToInt(3, receiveToDate);
            byte   code      = receiveToDate[2];

            if (code == PasswordCode._fileHeadCode)//说明发送过来的是文件是否传输的包头信息
            {
                FileStream fs        = null;
                FileState  stateHead = EncryptionDecryptFile.FileHeadDecrypt(receiveToDate, fs);
                string     fileName  = ReceiveMust.ReceiveOrNo(stateHead.FileLabel, stateHead.FileName, stateHead.FileLenth);
                if (fileName == "")
                {
                    haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileRefuse, fileLabel);
                }
                else
                {
                    fs = EngineTool.FileStreamWrite(fileName);
                    if (fs == null)
                    {
                        haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileRefuse, fileLabel);
                    }
                    else
                    {
                        ExpandBuffer(stateHead, stateOne);
                        stateHead.FileName = fileName; stateHead.Filestream = fs; stateHead.StateFile = 1;

                        FS.Add(stateHead);
                        haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileOk, fileLabel);
                    }
                }
                return(haveDate);
            }
            FileState state = FileLabelToState(fileLabel);

            if (state == null)
            {
                if (code == PasswordCode._fileCancel)
                {
                    return(null);
                }
                else
                {
                    return(EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileCancel, fileLabel));
                }
            }
            else
            {
                switch (code)
                {
                case PasswordCode._fileSubjectCode:           //收到的是文件主体代码
                    haveDate = EncryptionDecryptFile.FileSubjectDecrypt(state, receiveToDate);
                    ReceiveMust.FileProgress(state);          //输出文件进度
                    if (state.FileOkLenth >= state.FileLenth) //说明文件接收完成了
                    {
                        FileRemove(fileLabel); ReceiveMust.ReceiveSuccess(fileLabel);
                    }
                    break;

                case PasswordCode._fileCancel:    //对方已经取消了这个文件;
                    FileRemove(fileLabel);
                    ReceiveMust.FileCancel(fileLabel);
                    break;

                case PasswordCode._sendStop:    //对方暂停发送
                    FileStopIn(state, ReceiveMust);
                    break;

                case PasswordCode._fileContinue:                             //对方发过来一个续传的确认信息;你是否同意;
                    ExpandBuffer(state, stateOne);                           //有可能是stateOne换过了;
                    FileStopIn(state, ReceiveMust);                          //对方已经暂停了这个文件;我这边肯定也要先暂停掉
                    bool orStop = ReceiveMust.FileOrNotContingue(fileLabel); //让客户确认;是否续传
                    if (orStop)
                    {
                        haveDate        = EncryptionDecryptFile.ReceiveContingueEncryption(PasswordCode._fileContinueOk, state);
                        state.StateFile = 1;
                        ReceiveMust.FileContinue(fileLabel);
                    }
                    else
                    {
                        haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileContinueNo, fileLabel);
                    }
                    break;

                case PasswordCode._fileContinueOk:    //对方同意续传
                    state.StateFile = 1;
                    ReceiveMust.FileContinue(fileLabel);
                    haveDate = EncryptionDecryptFile.FileSubjectDecrypt(state, receiveToDate);
                    if (state.FileOkLenth >= state.FileLenth)    //说明文件接收完成了
                    {
                        FileRemove(fileLabel); ReceiveMust.ReceiveSuccess(fileLabel);
                    }
                    break;

                case PasswordCode._fileContinueNo:    //对方拒绝续传
                    ReceiveMust.FileNoContinue(fileLabel);
                    FileStopIn(state, ReceiveMust);
                    break;
                }
            }
            return(haveDate);
        }