Exemple #1
0
        /// <summary>
        /// читает одно сообщение от клиента
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        private byte[] ReadMessage()
        {
            byte[] buffer;
            bool   nullBuff;

            do
            {
                nullBuff = false;
                byte[] btSize = new byte[4];
                int    bufPos = 0;
                try
                {
                    CryRd.Read(btSize, 0, 4);
                    buffer = new byte[ToInt(btSize)];

                    do
                    {
                        int bufSize = 1048576;
                        if (bufSize > buffer.Length - bufPos)
                        {
                            bufSize = buffer.Length - bufPos;
                        }

                        bufPos += CryRd.Read(buffer, bufPos, bufSize);
                    } while (buffer.Length - bufPos != 0);

                    if (buffer.Length > 4)
                    {
                        if (buffer[4] == 0)
                        {
                            nullBuff = true;
                        }
                    }
                }
                catch (SocketException)
                {
                    //MessageBox.Show(e.Message);
                    buffer = null;
                }
                catch (CryptographicException)
                {
                    //MessageBox.Show(e.Message);
                    buffer = null;
                }
                catch (IOException)
                {
                    //MessageBox.Show(e.Message);
                    buffer = null;
                }
            } while (nullBuff);

            return(buffer);
        }
Exemple #2
0
        /// <summary>
        /// Запрашивает архив с сервера
        /// </summary>
        /// <param name="path">путь, куда будет распакован и расшифрован файл</param>
        /// <param name="name">имя файла</param>
        public void SRVGetFile(FrmWait frmWait, string path, string name)
        {
            if (MyKey == null)
            {
                MessageBox.Show("Введите пароль");
                return;
            }


            // сразу проверим, сможем ли мы записать в файл данные, иначе скачивать бессмысленно
            if (File.Exists(path))
            {
                if ((File.GetAttributes(path) != FileAttributes.Normal) &&
                    (File.GetAttributes(path) != FileAttributes.Archive))
                {
                    MessageBox.Show("Невозможно перезаписать файл");
                    return;
                }
            }


            SendResponse("GET ", name);

            while (lastRec == null)
            {
                Thread.Sleep(100);
            }

            int tmpSize = 10 * 2;                //берём в строку только первые 10 символов

            if (tmpSize > lastRec.Length)
            {
                tmpSize = lastRec.Length;                           //или сколько там этих символов есть в сообщении
            }
            string tmpStr  = Encoding.Unicode.GetString(lastRec, 0, tmpSize);
            string command = tmpStr.Split(' ')[0];


            int posData;
            int sizeFile;

            byte[] ThisKey;


            if (command == "FILE")
            {
                ThisKey  = MyKey;
                sizeFile = ToInt(lastRec, 5 * 2);  //размер файла
                posData  = 5 * 2 + 4;
            }
            else if (command == "FILEPSWD")
            {
                ThisKey = new byte[0x20];
                for (int i = 0; i < 0x20; i++)
                {
                    ThisKey[i] = lastRec[9 * 2 + i];
                }

                sizeFile = ToInt(lastRec, 9 * 2 + 0x20);  //размер файла
                posData  = 9 * 2 + 4 + 0x20;
            }
            else
            {
                frmWait.End();
                lastRec = null;
                MessageBox.Show("Не удалось скачать файл");
                return;
            }


            try
            {
                using (FileStream inFS = File.Create(path))
                {
                    using (RijndaelManaged rijAlg = new RijndaelManaged())
                    {
                        rijAlg.Key = ThisKey;
                        rijAlg.IV  = MyIV;

                        ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
                        using (CryptoStream csDecrypt = new CryptoStream(inFS, decryptor, CryptoStreamMode.Write))
                        {
                            MemoryStream msInp  = new MemoryStream(lastRec, posData, lastRec.Length - posData);
                            FileStream   fsTemp = File.Create(path + ".tmp");

                            byte[] bfsize = new byte[4];
                            CryRd.Read(bfsize, 0, 4);
                            int allFileSize = ToInt(bfsize);

                            int rec = 0;
                            while (rec < allFileSize)
                            {
                                int    bufSize = Math.Min(1048576, allFileSize - rec);
                                byte[] bufFile = new byte[bufSize];

                                CryRd.Read(bufFile, 0, bufSize);
                                fsTemp.Write(bufFile, 0, bufSize);
                                rec += bufSize;
                            }

                            fsTemp.Seek(0, SeekOrigin.Begin);
                            msInp.Seek(0, SeekOrigin.Begin);
                            ZipStorer zip = ZipStorer.Open(fsTemp, FileAccess.Read);
                            List <ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();
                            zip.ExtractFile(dir[0], csDecrypt);
                            zip.Close();
                            zip.Dispose();

                            fsTemp.Close();
                            File.Delete(path + ".tmp");
                            msInp.Dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                frmWait.End();
                lastRec = null;
                GC.Collect();
                MessageBox.Show(e.Message);
                return;
            }

            lastRec = null;
            GC.Collect();

            frmWait.End();
            MessageBox.Show("Файл " + name + " успешно скачан");
        }