示例#1
0
        private void DecryptFile(FileEncryptionResult encryptionResult)
        {
            try
            {
                var sourceFileName = encryptionResult.EncryptedFilePath;

                this.Info("[{0}]: Deciphering name...", sourceFileName);
                using (var inFile = File.OpenRead(sourceFileName))
                {
                    encryptionResult.UnencryptedFilePath = this.fileNameManager.ReadOriginalFileNameFromStream(inFile);
                    this.Info("[{0}]: Decrypting...", encryptionResult.UnencryptedFilePath);

                    var targetDirectory = Path.GetDirectoryName(encryptionResult.UnencryptedFilePath);
                    if (targetDirectory == null)
                    {
                        throw new Exception("Unexpected error - cannot get directory name from file path.");
                    }

                    if (Path.IsPathRooted(targetDirectory))
                    {
                        targetDirectory = targetDirectory.Substring(
                            targetDirectory.IndexOf(Path.DirectorySeparatorChar) + 1);
                    }

                    var decryptedFileLocation = this.configurationProvider.Configuration.Vaults
                                                .Single(x => x.Name == this.vaultName).DecryptedFileLocation;

                    targetDirectory = Path.Combine(decryptedFileLocation, targetDirectory);
                    if (!Directory.Exists(targetDirectory))
                    {
                        Directory.CreateDirectory(targetDirectory);
                    }

                    encryptionResult.UnencryptedFilePath = Path.Combine(
                        targetDirectory, Path.GetFileName(encryptionResult.UnencryptedFilePath));

                    using (var outFile = File.OpenWrite(encryptionResult.UnencryptedFilePath))
                        this.symmetricEncryptor.Decrypt(this.parameters, inFile, outFile);
                }

                encryptionResult.IsDone = true;
                this.Info("[{0}]: Decrypted.", encryptionResult.UnencryptedFilePath);
            }
            catch (Exception ex)
            {
                encryptionResult.Exception = ex;
                this.Error("[{0}]: Could not decrypt.", encryptionResult.EncryptedFilePath);
            }
        }
示例#2
0
        private void EncryptFile(FileEncryptionResult encryptionResult)
        {
            if (encryptionResult.IsDone)
            {
                return;
            }
            try
            {
                var encryptedFileLocation = this.configurationProvider.Configuration.Vaults
                                            .Single(x => x.Name == this.vaultName).EncryptedFileLocation;

                encryptionResult.EncryptedFilePath = Path.Combine(encryptedFileLocation,
                                                                  this.fileNameManager.GenerateNameForEncryptedFile(encryptionResult.UnencryptedFilePath));

                if (File.Exists(encryptionResult.EncryptedFilePath))
                {
                    File.Delete(encryptionResult.EncryptedFilePath);
                }

                this.Info("[{0}]: Encrypting...", encryptionResult.UnencryptedFilePath);
                using (FileStream
                       inFile = File.OpenRead(encryptionResult.UnencryptedFilePath),
                       outFile = File.OpenWrite(encryptionResult.EncryptedFilePath))
                {
                    this.fileNameManager.WriteOriginalFileNameToStream(encryptionResult.UnencryptedFilePath, outFile);
                    this.symmetricEncryptor.Encrypt(this.parameters, inFile, outFile);
                }

                encryptionResult.IsDone = true;
                this.Info("[{0}]: Encrypted.", encryptionResult.UnencryptedFilePath);
            }
            catch (Exception ex)
            {
                encryptionResult.Exception = ex;
                this.Error("[{0}]: Could not encrypt.");
            }
        }