Beispiel #1
0
        /// <summary>
        /// Loads contents of encryptedInputFile to string, decrypts the string and returns decrypted string to caller.
        /// </summary>
        /// <param name="encryptedInputFile">Path to file containing encrypted data.</param>
        /// <returns>Decrypted string.</returns>
        public string Decrypt(string encryptedInputFile)
        {
            string encryptedString = string.Empty;
            string decryptedString = string.Empty;
            //IStringEncryptor encryptor = new StringEncryptorAES();
            PFStringEncryptor encryptor = new PFStringEncryptor(_encryptionAlgorithm);

            if (String.IsNullOrEmpty(encryptedInputFile))
            {
                throw new ArgumentNullException("Path to both the encrypted input file needs to be specified.");
            }
            if (KeyIsValid(GetStringFromByteArray(_key)) == false)
            {
                throw new System.Exception("Invalid length for Key.");
            }
            if (IVIsValid(GetStringFromByteArray(_iv)) == false)
            {
                throw new System.Exception("Invalid length for IV.");
            }


            try
            {
                encryptedString = File.ReadAllText(encryptedInputFile);
                encryptor.Key   = this.Key;
                encryptor.IV    = this.IV;
                decryptedString = encryptor.Decrypt(encryptedString);
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Attempt to decrypt file to string failed: Make sure you are using the same key and IV used to encryp ");
                _msg.Append(encryptedInputFile);
                _msg.Append("\r\n");
                _msg.Append("Error Message:");
                _msg.Append("\r\n");
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }


            return(decryptedString);
        }
Beispiel #2
0
        /// <summary>
        /// Encrypts by loading inputFile to a string and then encrypting the string. String is then returned to the caller.
        /// </summary>
        /// <param name="inputFile">Full path to file that will be encrypted.</param>
        /// <returns>Returns encrypted string.</returns>
        public string Encrypt(string inputFile)
        {
            //IStringEncryptor encryptor = new StringEncryptorAES();
            PFStringEncryptor encryptor     = new PFStringEncryptor(_encryptionAlgorithm);
            string            encryptedData = string.Empty;

            if (String.IsNullOrEmpty(inputFile))
            {
                throw new ArgumentNullException("Path to both input file must be specified.");
            }

            if (KeyIsValid(GetStringFromByteArray(_key)) == false)
            {
                throw new System.Exception("Invalid length for Key.");
            }
            if (IVIsValid(GetStringFromByteArray(_iv)) == false)
            {
                throw new System.Exception("Invalid length for IV.");
            }


            try
            {
                string data = File.ReadAllText(inputFile);
                encryptor.Key = this.Key;
                encryptor.IV  = this.IV;
                encryptedData = encryptor.Encrypt(data);
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Attempt to encrypt file ");
                _msg.Append(inputFile);
                _msg.Append(" failed with following message: ");
                _msg.Append("\r\n");
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }


            return(encryptedData);
        }
        /// <summary>
        /// Decrypts by loading encryptedInputFile to a string and then decrypting the string. Decrypted string is then written out as text to the outputFile.
        /// </summary>
        /// <param name="encryptedInputFile">Full path to file containing the encrypted data.</param>
        /// <param name="outputFile">Full path to file that will contain decrypted data.</param>
        /// <returns>Returns path to output file.</returns>
        public string Decrypt(string encryptedInputFile, string outputFile)
        {
            //IStringEncryptor encryptor = new StringEncryptorAES();
            PFStringEncryptor encryptor = new PFStringEncryptor(_encryptionAlgorithm);

            if (String.IsNullOrEmpty(encryptedInputFile) || String.IsNullOrEmpty(outputFile))
            {
                throw new ArgumentNullException("Paths to both the encrypted input file and the output file need to be specified.");
            }

            if (KeyIsValid(GetStringFromByteArray(_key)) == false)
            {
                throw new System.Exception("Invalid length for Key.");
            }
            if (IVIsValid(GetStringFromByteArray(_iv)) == false)
            {
                throw new System.Exception("Invalid length for IV.");
            }


            try
            {
                string encryptedData = File.ReadAllText(encryptedInputFile);
                encryptor.Key = this.Key;
                encryptor.IV  = this.IV;
                string decryptedData = encryptor.Decrypt(encryptedData);
                File.WriteAllText(outputFile, decryptedData);
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Attempt to decrypt file ");
                _msg.Append(encryptedInputFile);
                _msg.Append(" failed. Verify you are using same key/iv pair used to encrypt.  Error message: ");
                _msg.Append("\r\n");
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }


            return(outputFile);
        }