private void ThrdDelete(object obj) { FrmWait frmWait = obj as FrmWait; if (TrData.Login.Length != 0) { string name; if (listFileUser.SelectedIndex != -1) { name = listFileUser.GetItemText(listFileUser.SelectedItem); } else { frmWait.End(); MessageBox.Show("Выберите файл для удаления(можно удалять только свои файлы)"); return; } TrData.SRVDelete(name); frmWait.End(); } else { frmWait.End(); MessageBox.Show("Введите логин и нажмите кнопку Вход"); } }
private void ThrdSendFile(object obj) { FrmWait frmWait = obj as FrmWait; TrData.SRVSendFile(frmWait, openFileDlg.FileName, openFileDlg.SafeFileName); frmWait.End(); }
private void ThrdLoadFile(object obj) { object[] parms = obj as object[]; FrmWait frmWait = parms[0] as FrmWait; if (TrData.Login.Length != 0) { string servname = parms[1] as string; //сюда поместим имя файла, как он зовётся на сервере string fname = parms[2] as string; TrData.SRVGetFile(frmWait, fname, servname); frmWait.End(); } else { frmWait.End(); MessageBox.Show("Введите логин и нажмите кнопку Вход"); } }
/// <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 + " успешно скачан"); }
public bool SRVSendFile(FrmWait frmWait, string path, string name) { byte[] bufFile; int FileSize; if (MyKey == null) { frmWait.End(); MessageBox.Show("Введите пароль"); return(false); } //шифруем файл using (RijndaelManaged rijAlg = new RijndaelManaged()) { rijAlg.Key = MyKey; rijAlg.IV = MyIV; ICryptoTransform encryptor = rijAlg.CreateEncryptor(); try { using (FileStream inFS = File.OpenRead(path)) { using (CryptoStream csRijnd = new CryptoStream(inFS, encryptor, CryptoStreamMode.Read)) { byte[] commFile = Encoding.Unicode.GetBytes("FILE "); byte[] nameFile = Encoding.Unicode.GetBytes(name); byte[] sizeName = { (byte)name.Length }; FileSize = (int)inFS.Length; int allBufSize = (int)(inFS.Length & 0xFFFFFFF0) + 0x10; bufFile = new byte[1048576]; //нужно округлять размер до границы в 0x10 int sizeMess = commFile.Length + 1 + nameFile.Length + 4; // +allBufSize; try { //первое сообщение с инфой о файле CryWr.Write(ToAByte(sizeMess), 0, 4); //размер вcего сообщения CryWr.Write(commFile, 0, commFile.Length); //команда CryWr.Write(sizeName, 0, 1); //длина имени файла CryWr.Write(nameFile, 0, nameFile.Length); //имя файла CryWr.Write(ToAByte(FileSize), 0, 4); //длина файла(до шифрования) //второе сообщение с файлом CryWr.Write(ToAByte(allBufSize), 0, 4); //размер вcего файла int rec = 0; while (rec < allBufSize) { int bufSize = Math.Min(1048576, allBufSize - rec); csRijnd.Read(bufFile, 0, bufSize); CryWr.Write(bufFile, 0, bufSize); //данные файла rec += bufSize; } iWr += 4 + sizeMess + 4 + allBufSize; RoundWrStream(); } catch (IOException e) { frmWait.End(); MessageBox.Show(e.Message); } } } } catch (IOException e) { frmWait.End(); MessageBox.Show(e.Message); return(false); } } while (lastRec == null) //ждём ответа от сервера { Thread.Sleep(100); } int tmpSize = 8 * 2; //берём в строку только первые 8 символов if (tmpSize > lastRec.Length) { tmpSize = lastRec.Length; //или сколько там этих символов есть в сообщении } string tmpStr = Encoding.Unicode.GetString(lastRec, 0, tmpSize); string command = tmpStr.Split(' ')[0]; lastRec = null; frmWait.End(); if (command == "FILE_OK") { return(true); } else if (command == "FILE_ERR") { return(false); } return(false); }