コード例 #1
0
        /// <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));
        }
コード例 #2
0
        /// <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
            });
        }