Esempio n. 1
0
        protected override void Execute(CodeActivityContext context)
        {
            try
            {
                string   inputFilePath  = InputFilePath.Get(context);
                string   outputFilePath = OutputFilePath.Get(context);
                string   key            = Key.Get(context);
                Encoding keyEncoding    = KeyEncoding.Get(context);

                if (string.IsNullOrWhiteSpace(inputFilePath))
                {
                    throw new ArgumentNullException(Resources.InputFilePathDisplayName);
                }
                if (string.IsNullOrWhiteSpace(outputFilePath))
                {
                    throw new ArgumentNullException(Resources.OutputFilePathDisplayName);
                }
                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new ArgumentNullException(Resources.Key);
                }
                if (keyEncoding == null)
                {
                    throw new ArgumentNullException(Resources.Encoding);
                }
                if (!File.Exists(inputFilePath))
                {
                    throw new ArgumentException(Resources.FileDoesNotExistsException, Resources.InputFilePathDisplayName);
                }
                // Because we use File.WriteAllText below, we don't need to delete the file now.
                if (File.Exists(outputFilePath) && !Overwrite)
                {
                    throw new ArgumentException(Resources.FileAlreadyExistsException, Resources.OutputFilePathDisplayName);
                }

                byte[] encrypted = File.ReadAllBytes(inputFilePath);

                byte[] decrypted = null;
                try
                {
                    decrypted = CryptographyHelper.DecryptData(Algorithm, encrypted, keyEncoding.GetBytes(key));
                }
                catch (CryptographicException ex)
                {
                    throw new InvalidOperationException(Resources.GenericCryptographicException, ex);
                }

                // This overwrites the file if it already exists.
                File.WriteAllBytes(outputFilePath, decrypted);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                if (!ContinueOnError.Get(context))
                {
                    throw;
                }
            }
        }
Esempio n. 2
0
        protected override void Execute(CodeActivityContext context)
        {
            Result.Set(context, "Not Masked");
            string inputFilePath   = InputFilePath.Get(context);
            string outputDirPath   = OutputDirPath.Get(context);
            string maskingKeyWord  = MaskingKeyWord.Get(context);
            string occurrenceValue = Occurrence.Get(context);
            bool   maskOnlyKeyword = MaskOnlyKeyword.Get(context);

            if (!string.IsNullOrEmpty(inputFilePath) && !string.IsNullOrEmpty(outputDirPath) && !string.IsNullOrEmpty(maskingKeyWord) &&
                maskingKeyWord.IndexOf(" ") < 0 && !string.IsNullOrEmpty(occurrenceValue) && File.Exists(inputFilePath) &&
                Directory.Exists(outputDirPath))
            {
                string redactedPDFNameEscaped = Regex.Replace(Path.GetFileName(inputFilePath), "%", "%%");
                string redactedPDFPath        = $@"{outputDirPath}\{redactedPDFNameEscaped}";
                string jpgFilesDir            = ConvertPDFToJPGs(inputFilePath);
                if (maskOnlyKeyword)
                {
                    if (!string.IsNullOrEmpty(occurrenceValue) && occurrenceValue.Equals("ALL", StringComparison.OrdinalIgnoreCase))
                    {
                        int occurrence       = 0;
                        var redactedJPGFiles = RedactOnlyKeyword(jpgFilesDir, maskingKeyWord, occurrence, maskOnlyKeyword);
                        if (maskingSuccessfull)
                        {
                            ConvertJPGsToPDF(redactedJPGFiles, redactedPDFPath);
                            Result.Set(context, "Success");
                        }
                    }
                    else if (!string.IsNullOrEmpty(occurrenceValue))
                    {
                        if (IsDigitsOnly(occurrenceValue))
                        {
                            int occurrence       = Convert.ToInt32(occurrenceValue);
                            var redactedJPGFiles = RedactOnlyKeyword(jpgFilesDir, maskingKeyWord, occurrence, maskOnlyKeyword);
                            if (maskingSuccessfull)
                            {
                                ConvertJPGsToPDF(redactedJPGFiles, redactedPDFPath);
                                Result.Set(context, "Success");
                            }
                        }
                        else
                        {
                            throw new Exception("Occurrence should contain only digits.");
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(occurrenceValue))
                {
                    if (IsDigitsOnly(occurrenceValue))
                    {
                        int occurrence       = Convert.ToInt32(occurrenceValue);
                        var redactedJPGFiles = RedactTextTillEnd(jpgFilesDir, maskingKeyWord, occurrence, maskOnlyKeyword);
                        if (maskingSuccessfull)
                        {
                            ConvertJPGsToPDF(redactedJPGFiles, redactedPDFPath);
                            Result.Set(context, "Success");
                        }
                    }
                    else
                    {
                        throw new Exception("Occurrence should contain only digits.");
                    }
                }
                Directory.Delete(jpgFilesDir, recursive: true);
            }
            else
            {
                if (!string.IsNullOrEmpty(inputFilePath) && File.Exists(inputFilePath))
                {
                    throw new Exception("Input File Path is not correct.");
                }
                else if (!string.IsNullOrEmpty(outputDirPath) && Directory.Exists(outputDirPath))
                {
                    throw new Exception("Directory Path is not correct.");
                }
                else if (!string.IsNullOrEmpty(maskingKeyWord) && maskingKeyWord.IndexOf(" ") < 0)
                {
                    throw new Exception("Masking Keyword is not correct.");
                }
                else if (!string.IsNullOrEmpty(occurrenceValue))
                {
                    throw new Exception("Occurrence Value should not be null.");
                }
            }
        }