public static void Encrypt(ImageViewModel image, string OutFilePath, string password) { int len; byte[] buffer = new byte[4096]; string originalImagePath = image.AbsoluteMasterPath; using (FileStream outfs = new FileStream(OutFilePath, FileMode.Create, FileAccess.Write)) { using (AesManaged aes = new AesManaged()) { aes.BlockSize = 128; aes.KeySize = 128; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, 16); byte[] salt = new byte[16]; salt = deriveBytes.Salt; byte[] bufferKey = deriveBytes.GetBytes(16); aes.Key = bufferKey; aes.GenerateIV(); ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); using (CryptoStream cse = new CryptoStream(outfs, encryptor, CryptoStreamMode.Write)) { outfs.Write(salt, 0, 16); outfs.Write(aes.IV, 0, 16); using (FileStream fs = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read)) { while ((len = fs.Read(buffer, 0, 4096)) > 0) { cse.Write(buffer, 0, len); } } } } } EncryptImage encryptImage = new EncryptImage(); encryptImage.ID = Guid.NewGuid(); encryptImage.TargetImageID = image.ID; encryptImage.EncryptFilePath = OutFilePath; EncryptImageDao dao = new EncryptImageDao(); dao.InsertOrReplace(encryptImage); }
public override void ConfigureTaskImplementation(AsyncTaskSequence sequence) { var books = LibraryManager.Value.BookSource; foreach (var book in books) { ContentsLoadTask.FillContents(LibraryManager.Value, book); var images = book.Contents; foreach (var image in images) { ContentsLoadTask.Load(image); sequence.Add(() => _TargetEncryptImage = EncryptImageFacade.FindBy(image.Image.ID)); sequence.Add(() => Encryptor.Decrypt(_TargetEncryptImage.EncryptFilePath, image.Image.AbsoluteMasterPath, Password)); sequence.Add(() => File.Delete($"{Configuration.ApplicationConfiguration.WorkingDirectory}\\{Specifications.MASTER_DIRECTORY}\\{image.Image.ID}{Path.GetExtension(image.Image.AbsoluteMasterPath)}")); sequence.Add(() => EncryptImageFacade.DeleteBy(image.Image.ID)); } } sequence.Add(() => OnmemoryImageManager.Instance.Clear()); sequence.Add(() => Configuration.ApplicationConfiguration.Password = null); sequence.Add(() => Configuration.ApplicationConfiguration.LibraryIsEncrypted = false); }