private bool TryDecrypt(DataContract message, out DecryptedData decryptedData, string passwordHash) { decryptedData = null; var keyPaths = this.GetAllPgpKeysForUserId(message.UserId); var directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); var inputFilePath = Path.Combine(directory, "message.pgp"); var outputFilePath = Path.Combine(directory, "message__decrypted"); foreach (var path in keyPaths) { File.WriteAllBytes(inputFilePath, Convert.FromBase64String(message.EncryptedData)); var isSuccessful = DataProtector.DecryptFile(inputFilePath, outputFilePath, path, passwordHash); if (isSuccessful) { decryptedData = JsonConvert.DeserializeObject <DecryptedData>(File.ReadAllText(outputFilePath)); File.Delete(inputFilePath); File.Delete(outputFilePath); return(true); } } if (File.Exists(inputFilePath)) { File.Delete(inputFilePath); } if (File.Exists(outputFilePath)) { File.Delete(outputFilePath); } return(false); }