Example #1
0
        public static Stream JustDecryptAndDecompress(FileInfo file, string password, string salt)
        {
            MemoryStream decompressedStream = new MemoryStream();

            using (FileStream source = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (StreamCryptoWrapper decryptedSource = StreamCryptoWrapper.Decrypt(source, password, salt))
                    using (MemoryStream decryptedMemory = new MemoryStream())
                    {
                        decryptedSource.CryptoStream.CopyTo(decryptedMemory);
                        decryptedMemory.Position = 0;
                        KeepLzmaCompressor.Decompress(decryptedMemory, decompressedStream);
                        decompressedStream.Position = 0;
                    }
            return(decompressedStream);
        }
Example #2
0
        public override void Execute()
        {
            EnsurePathExists(PendingPath);

            try
            {
                if (_Mode == ModeEnum.CompressEncrypt)
                {
                    Program.log.Debug("starting backup of " + FromPath);

                    Program.log.Debug("=> compress and encrypt to " + PendingPath);

                    using (Stream encryptedStream = GetEncryptedStream())
                        using (StreamCryptoWrapper encryptedTarget = StreamCryptoWrapper.Encrypt(encryptedStream, _Password, _Salt))
                        {
                            using (Stream compressedStream = GetCompressedStream())
                            {
                                using (FileStream source = new FileStream(FromPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                                    KeepLzmaCompressor.Compress(source, compressedStream);

                                compressedStream.Position = 0;
                                Program.log.Debug("compression done, start encrypting");
                                compressedStream.CopyTo(encryptedTarget.CryptoStream);
                                encryptedTarget.CryptoStream.FlushFinalBlock();
                                Program.log.Debug("encryption done, hashing result");
                            }

                            encryptedStream.Position = 0;
                            ChecksumTarget           = KeepHasher.GetHash(encryptedStream);
                            encryptedStream.Position = 0;
                            SizeTarget = encryptedStream.Length;
                            Program.log.Debug("done");

                            if (encryptedStream is MemoryStream)
                            {
                                using (Stream target = new FileStream(PendingPath, FileMode.Create, FileAccess.Write, FileShare.Read))
                                    encryptedStream.CopyTo(target);
                            }
                        }
                }
                else if (_Mode == ModeEnum.DecryptDecompress)
                {
                    Program.log.Debug("<= decrypt and decompress " + FromPath);
                    using (FileStream source = new FileStream(FromPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        using (StreamCryptoWrapper decryptedSource = StreamCryptoWrapper.Decrypt(source, _Password, _Salt))
                            using (Stream decryptedMemory = GetDecryptedStream())
                                using (FileStream target = new FileStream(PendingPath, FileMode.Create, FileAccess.Write, FileShare.Read))
                                {
                                    decryptedSource.CryptoStream.CopyTo(decryptedMemory);
                                    decryptedMemory.Position = 0;
                                    Program.log.Debug("decryption to memory done, decompressing to disc");
                                    KeepLzmaCompressor.Decompress(decryptedMemory, target);
                                    Program.log.Debug("decompression done");
                                }
                }
            }
            finally
            {
                foreach (string file in _TempFilesToDelete)
                {
                    Program.log.Debug("deleting temp file " + file);
                    File.Delete(file);
                }
            }

            EnsurePathExists(ToPath);

            Program.log.Debug("renaming " + PendingPath + " -> " + ToPath);
            File.Move(PendingPath, ToPath);

            if (_Mode == ModeEnum.DecryptDecompress)
            {
                Program.log.Debug("restoring metadata " + ToPath);
                FileInfo fi = new FileInfo(ToPath);
                fi.LastWriteTimeUtc = SourceFile.LastWriteTimeUtc;
                fi.CreationTimeUtc  = SourceFile.CreationTimeUtc;
                if (fi.Length != SourceFile.SizeBytes)
                {
                    Program.log.Error("size mismatch " + ToPath + " " + SourceFile.Sha256);
                }

                //string sha = Hashing.GetHash(fi);
                //if (sha != SourceFile.Sha256)
                //    Program.log.Error("hash mismatch " + ToPath + " " + sha + " but should be "+ SourceFile.Sha256);
            }
        }