Esempio n. 1
0
        /// <summary>
        /// Factory method for creating instances of the string encryptor classes.
        /// </summary>
        /// <param name="alg">Type of encryption algorithm.</param>
        /// <returns>IStringEncryptor interface representing the chosen string encryptor.</returns>
        public static IStringEncryptor GetStringEncryptor(pfEncryptionAlgorithm alg)
        {
            IStringEncryptor enc;

            switch (alg)
            {
            case pfEncryptionAlgorithm.AES:
                enc = new StringEncryptorAES();
                break;

            case pfEncryptionAlgorithm.DES:
                enc = new StringEncryptorDES();
                break;

            case pfEncryptionAlgorithm.TripleDES:
                enc = new StringEncryptorTripleDES();
                break;

            default:
                enc = new StringEncryptorAES();
                break;
            }

            return(enc);
        }
        /// <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();

            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);
        }
        /// <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();

            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);
        }
        /// <summary>
        /// Encrypts by loading inputFile to a string and then encrypting the string. String is then written out as text to the encryptedOutputFile.
        /// </summary>
        /// <param name="inputFile">Full path to file that will be encrypted.</param>
        /// <param name="encryptedOutputFile">Full path to file that will contain encrypted data.</param>
        /// <returns>Returns path to encrypted output file.</returns>
        public string Encrypt(string inputFile, string encryptedOutputFile)
        {
            IStringEncryptor encryptor = new StringEncryptorAES();

            if (String.IsNullOrEmpty(inputFile) || String.IsNullOrEmpty(encryptedOutputFile))
            {
                throw new ArgumentNullException("Paths to both input and encrypted output file need to 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;
                string encryptedData = encryptor.Encrypt(data);
                File.WriteAllText(encryptedOutputFile, encryptedData);
            }
            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(encryptedOutputFile);
        }