public void AddToSecure(string _path) { List <FILEPATH> lf = jtp.Deserialize(); if (!(lf == null)) { foreach (var item in lf) { if (item.PrevFile == _path) { MessageBox.Show("That folder is already secured"); return; } } } else { lf = new List <FILEPATH>(); } a.Ziping(_path); string pathToZip = _path + ".zip"; byte[] fileBytes = File.ReadAllBytes(pathToZip); SHA1 sha = new SHA1CryptoServiceProvider(); byte[] hashBytes = sha.ComputeHash(fileBytes); var FileName = new StringBuilder(); foreach (byte b in hashBytes) { FileName.Append(b.ToString("x2")); } string newFileFullPath = FOLDER + "/" + FileName.ToString() + ".zip.tge"; paths = new FILEPATH() { NewFile = newFileFullPath, PrevFile = pathToZip }; password = FileName.ToString(); lf.Add(paths); jtp.Serialize(lf); Thread t = new Thread(StartEncrypting); t.Start(); }
private void bwEncrypt_DoWork(object sender, DoWorkEventArgs e) { if (!stop) { int x = 0; bwEncrypt.ReportProgress(0); try { if (Password != Confirmed) { stop = true; MessageBox.Show("Passwords doesnt match!!!"); return; } if (rbFile.Checked || rbFolder.Checked) { FileAttributes attr = File.GetAttributes(FilePath); if (attr.HasFlag(FileAttributes.Directory)) { string zipfile = aes.Ziping(FilePath); aes.FileEncrypt(zipfile, Password, null); File.Delete(zipfile); } else { aes.FileEncrypt(FilePath, Password, null); } //delete paretnt files if (rbDelete.Checked && !attr.HasFlag(FileAttributes.Directory)) { File.Delete(FilePath); } else if (rbDelete.Checked && attr.HasFlag(FileAttributes.Directory)) { Directory.Delete(FilePath, true); } bwEncrypt.ReportProgress(1); stop = true; } else if (rbDisc.Checked) { foreach (var item in files) { if (this.bwEncrypt.CancellationPending) { e.Cancel = true; return; } FileAttributes attr = File.GetAttributes(item); if (attr.HasFlag(FileAttributes.Directory)) { string zipfile = aes.Ziping(item); aes.FileEncrypt(zipfile, Password, null); File.Delete(zipfile); } else { aes.FileEncrypt(item, Password, null); } if (rbDelete.Checked && !attr.HasFlag(FileAttributes.Directory)) { File.Delete(item); } else if (rbDelete.Checked && attr.HasFlag(FileAttributes.Directory)) { Directory.Delete(item, true); } x++; bwEncrypt.ReportProgress(x); } stop = true; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { e.Cancel = true; bwEncrypt.CancelAsync(); } }