Exemplo n.º 1
0
        private void DecryptFile(string filePath, string password)
        {
            var fileInfo = new FileInfo(filePath);

            if (fileInfo.IsReadOnly)
            {
                fileInfo.IsReadOnly = false;
            }

            var decryptedFilePath = GetUniqFileName(filePath, ".dec");

            try
            {
                var metadata = EncryptionFactory.GetMetadata();

                metadata.Initialize(password);

                using (var fileStream = File.OpenRead(filePath))
                {
                    if (!metadata.TryReadFromStream(fileStream, Version))
                    {
                        return;
                    }

                    metadata.ComputeAndValidateHmacHash(fileStream);

                    using (var decryptedFileStream = new FileStream(decryptedFilePath, FileMode.Create))
                    {
                        using (var algorithm = metadata.GetCryptographyAlgorithm())
                        {
                            using (var transform = algorithm.CreateDecryptor())
                            {
                                using (var cryptoStream = new CryptoStreamWrapper(decryptedFileStream, transform, CryptoStreamMode.Write))
                                {
                                    fileStream.CopyTo(cryptoStream);

                                    cryptoStream.FlushFinalBlock();
                                    cryptoStream.Close();
                                }
                            }
                        }

                        decryptedFileStream.Close();
                    }

                    fileStream.Close();
                }

                ReplaceFile(decryptedFilePath, filePath);
            }
            catch (Exception exception)
            {
                if (File.Exists(decryptedFilePath))
                {
                    File.Delete(decryptedFilePath);
                }

                throw exception;
            }
        }
Exemplo n.º 2
0
        private void EncryptFile(string filePath, string password)
        {
            var fileInfo = new FileInfo(filePath);

            if (fileInfo.IsReadOnly)
            {
                fileInfo.IsReadOnly = false;
            }

            var ecryptedFilePath = GetUniqFileName(filePath, ".enc");

            try
            {
                var metadata = EncryptionFactory.GetMetadata();

                metadata.Initialize(Version, password, fileInfo.Length);

                using (var ecryptedFileStream = new FileStream(ecryptedFilePath, FileMode.Create))
                {
                    metadata.WriteToStream(ecryptedFileStream);

                    using (var algorithm = metadata.GetCryptographyAlgorithm())
                    {
                        using var transform    = algorithm.CreateEncryptor();
                        using var cryptoStream = new CryptoStreamWrapper(ecryptedFileStream, transform, CryptoStreamMode.Write);
                        using (var fileStream = File.OpenRead(filePath))
                        {
                            fileStream.CopyTo(cryptoStream);
                            fileStream.Close();
                        }

                        cryptoStream.FlushFinalBlock();

                        metadata.ComputeAndWriteHmacHash(ecryptedFileStream);

                        cryptoStream.Close();
                    }

                    ecryptedFileStream.Close();
                }

                ReplaceFile(ecryptedFilePath, filePath);
            }
            catch (Exception exception)
            {
                if (File.Exists(ecryptedFilePath))
                {
                    File.Delete(ecryptedFilePath);
                }

                throw exception;
            }
        }