Ejemplo n.º 1
0
        /// <summary>
        /// encrypt the stream symmetrically using the security profile
        /// information stored in the configuration file
        /// </summary>
        /// <param name="data">stream data</param>
        /// <param name="profile">profile name</param>
        /// <returns>stream data</returns>
        public static Stream Encrypt(Stream data, string profile)
        {
            SAF.Configuration.ConfigurationManager cm = (SAF.Configuration.ConfigurationManager)System.Configuration.ConfigurationManager.GetSection("Framework");
            CryptographyConfiguration cc = cm.CryptographyConfig;
            //retrieve security profile information
            XmlNode cryptoInfo = cc.SearchCryptoInfoByProfileName(profile);
            bool    symmetric  = Boolean.Parse(cryptoInfo.Attributes["symmetric"].Value);

            ICryptoTransform   encryptor = null;
            SymmetricAlgorithm provider  = null;

            if (symmetric != true)
            {
                throw new System.Exception("This method id not intended for asymmetric  encryption");
            }

            //retrive the secret key and iv information
            provider = cc.GetSymmetricAlgorithmProvider(profile);
            string key = cryptoInfo.SelectSingleNode("SecretKey").InnerText;
            string iv  = cryptoInfo.SelectSingleNode("IV").InnerText;

            provider.Key = Encoding.Default.GetBytes(key);
            provider.IV  = Encoding.Default.GetBytes(iv);

            encryptor = provider.CreateEncryptor();
            MemoryStream encrypted = new MemoryStream();
            //encrypt the stream symmetrically
            CryptoStream encStream = new CryptoStream(encrypted, encryptor, CryptoStreamMode.Write);

            byte[] buffer = new byte[1024];
            int    count  = 0;

            while ((count = data.Read(buffer, 0, 1024)) > 0)
            {
                encStream.Write(buffer, 0, count);
            }
            encStream.FlushFinalBlock();
            encrypted.Position = 0;
            return((Stream)encrypted);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// decrypt the stream data symmetrically using the security profile
        /// information stored in the configuration file
        /// </summary>
        /// <param name="data">stream data</param>
        /// <param name="profile">security profile name</param>
        /// <returns>decrypted stream</returns>
        public static Stream Decrypt(Stream data, string profile)
        {
            SAF.Configuration.ConfigurationManager cm = (SAF.Configuration.ConfigurationManager)System.Configuration.ConfigurationManager.GetSection("Framework");
            CryptographyConfiguration cc = cm.CryptographyConfig;
            //retrieve the security profile information for configuration file
            XmlNode cryptoInfo = cc.SearchCryptoInfoByProfileName(profile);
            bool    symmetric  = Boolean.Parse(cryptoInfo.Attributes["symmetric"].Value);

            ICryptoTransform   decryptor = null;
            SymmetricAlgorithm provider  = null;

            if (symmetric != true)
            {
                throw new System.Exception("This method id not intended for asymmetric  encryption");
            }
            //retrieve the secret key and iv from the configuration file
            provider = cc.GetSymmetricAlgorithmProvider(profile);
            string key = cryptoInfo.SelectSingleNode("SecretKey").InnerText;
            string iv  = cryptoInfo.SelectSingleNode("IV").InnerText;

            provider.Key = Encoding.Default.GetBytes(key);
            provider.IV  = Encoding.Default.GetBytes(iv);
            decryptor    = provider.CreateDecryptor();
            //decrypt the stream
            CryptoStream decStream = new CryptoStream(data, decryptor, CryptoStreamMode.Read);
            MemoryStream decrypted = new MemoryStream();

            byte[] buffer = new byte[2048];
            int    count  = 0;

            while ((count = decStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                decrypted.Write(buffer, 0, count);
            }

            decrypted.Position = 0;
            return((Stream)decrypted);
        }