/// <summary>
        /// Decrypt the data using the given key and passed IV
        /// </summary>
        public static string DecryptData(SymmetricCipherResults cipherResults,
                                         SymmetricAlgorithmTypeEnum symmetricAlgorithm,
                                         string key)
        {
            key = SymmetricOperation.MakeKeyLegalSize(symmetricAlgorithm, key);

            byte[] IV = Convert.FromBase64String(cipherResults.IV);

            string PlainText = SymmetricOperation.DecryptFromBase64(symmetricAlgorithm,
                                                                    cipherResults.CipherText, key, IV, System.Text.Encoding.UTF8);

            return(PlainText);
        }
        /// <summary>
        /// Create secure string.
        /// </summary>
        private string CreateSecureString()
        {
            //?? Create a new random salt for the specified length
            // Decrypt will ignore the first pre-fixed number of salt characters
            _salt = "AAAAABBB";

            // Put a new timestamp in the data to be encrypted
            lastEncryptTime = DateTime.Now;

            DateTime dtExpirationTim = lastEncryptTime + _expirationTimeSpan;

            String sPlainStr = string.Format("{0}{1}{2:yyyyMMddHHmmss}",
                                             _salt, _plainString, dtExpirationTim);

            return(SymmetricOperation.EncryptToBase64(AlgorithmType, sPlainStr,
                                                      _secretKey, InitVector, ASCIIEncoding.ASCII));
        }
        /// <summary>
        /// Encrypt the data using the symmetric algorithm provided and key. Generates the Initialization Vector.
        /// Returns the IV and the encrypted string
        /// </summary>
        public static SymmetricCipherResults EncryptData(string plainText,
                                                         SymmetricAlgorithmTypeEnum symmetricAlgorithm,
                                                         string key)
        {
            SymmetricAlgorithm algorithm = SymmetricOperation.CreateSymmetricAlgorithmProvider(symmetricAlgorithm);

            key = SymmetricOperation.MakeKeyLegalSize(symmetricAlgorithm, key);
            algorithm.GenerateIV();
            string iv = Convert.ToBase64String(algorithm.IV);

            string cipherText = SymmetricOperation.EncryptToBase64(symmetricAlgorithm,
                                                                   plainText, key, algorithm.IV, System.Text.Encoding.UTF8);

            return(new SymmetricCipherResults()
            {
                CipherText = cipherText, IV = iv
            });
        }
        /// <summary>
        /// Decrypt data from the secure string.
        /// </summary>
        public static string GetDataFromSecureString(string secretKey, byte[] bufInitVector, string base64Data)
        {
            secretKey = SymmetricOperation.MakeKeyLegalSize(AlgorithmType, secretKey);

            // Decrypt the secret string and make sure that is not expired
            string plainStr = SymmetricOperation.DecryptFromBase64(AlgorithmType,
                                                                   base64Data, secretKey, bufInitVector, ASCIIEncoding.ASCII);

            //Parse data to remove the salt and the expiration timestamp
            plainStr = plainStr.Substring(SaltLength);
            string   sExpirationTim  = plainStr.Substring(plainStr.Length - 14);
            DateTime dtExpirationTim = DateTime.ParseExact(sExpirationTim,
                                                           "yyyyMMddHHmmss",
                                                           System.Globalization.DateTimeFormatInfo.InvariantInfo);

            // Check if the contetn should expire
            if (dtExpirationTim > DateTime.Now)
            {
                return(plainStr.Substring(0, plainStr.Length - 14));
            }

            return(string.Empty);
            //Bad format
        }
 /// <summary>
 /// This function uses the symmetric AES algorithm to encrypt data (the key and IV are the one which is already
 /// sent to the receiver in the first phase as session key.
 /// </summary>
 public byte[] EncodeMessage(byte[] bufData)
 {
     return(SymmetricOperation.EncryptData(_aes, bufData));
 }
 /// <summary>
 /// This functon decodes the messages using the AES provider initialized when envelope was decoded.
 /// </summary>
 public byte[] DecodeMessage(byte[] cypherText)
 {
     return(SymmetricOperation.DecryptData(_aes, cypherText));
 }
 /// <summary>
 /// EncryptedString constructor.
 /// </summary>
 public EncryptedString(string secretKey, string plainStr, Int32 expirationIntervalMin)
 {
     _secretKey          = SymmetricOperation.MakeKeyLegalSize(AlgorithmType, secretKey);
     _plainString        = plainStr;
     _expirationTimeSpan = new TimeSpan(0, expirationIntervalMin, 0);
 }