public static async Task <Result> DecryptFileAsync( string encryptedFilePath, string privateKeyXmlFilePath, string encryptedFileInfoXmlFilePath) { Result decryptResult; try { var deserializationResult = EncryptedFileInfo.ReadFromFile(encryptedFileInfoXmlFilePath); if (deserializationResult.Failure) { return(Result.Fail( "An error occurred reading the encryption info XML file, unable to continue decrypting file.")); } var encryptionInfoXml = deserializationResult.Value; var privateKeyXml = CryptoKeys.ReadXmlKeyFromFile(privateKeyXmlFilePath); decryptResult = await Task.Factory.StartNew(() => Decrypt(encryptedFilePath, encryptionInfoXml, privateKeyXml)).ConfigureAwait(false); } catch (Exception ex) { return(Result.Fail($"{ex.Message} {ex.GetType()}")); } return(decryptResult); }
public static async Task <Result <EncryptedFileInfo> > EncryptFileAsync( string filePath, string publicKeyXmlFilePath, HashAlgorithmType hashAlgorithm = HashAlgorithmType.SHA2_256) { var folderPath = Path.GetDirectoryName(filePath); var encryptedFileName = $"{Path.GetFileName(filePath)}.encrypted"; var infoXmlFilePath = Path.Combine(folderPath, $"{encryptedFileName}.xml"); EncryptedFileInfo infoXml; try { var publicKeyXml = CryptoKeys.ReadXmlKeyFromFile(publicKeyXmlFilePath); infoXml = await Task.Factory.StartNew(() => Encrypt(filePath, publicKeyXml, hashAlgorithm)).ConfigureAwait(false); } catch (FileNotFoundException ex) { return(Result.Fail <EncryptedFileInfo>($"{ex.Message} {ex.GetType()}")); } var serializationResult = EncryptedFileInfo.SaveToFile(infoXml, infoXmlFilePath); return(serializationResult.Success ? Result.Ok(infoXml) : Result.Fail <EncryptedFileInfo>("Error occurred serializing encrypted file info to XML.")); }