Enterprise Library 4.0, Cryptography application block.

Topics: Cryptography Application Block
Sep 29, 2009 at 5:45 PM
Edited Sep 30, 2009 at 8:30 AM

Hi,

I am developing an application in which I like to use the Cryptography Application Block. But I dont want to use a config file in the application, in that case how I will get the "symmetricInstance" (A symmetric instance from configuration) parameter of "EncryptSymmetric" function to encrypt any data, as this values is from config file

Thanks!
Chetan Birari.

Sep 30, 2009 at 7:17 AM

Hi,

In that case I think you'll need to create everything programmatically. Please try this code if this works for you:

            const string providerName = "testProvider";

            //Create the Key
            RijndaelManaged algorithm = new RijndaelManaged();
            algorithm.GenerateIV();
            PasswordDeriveBytes password = new PasswordDeriveBytes("passwords", algorithm.IV);
            byte[] key = password.GetBytes(32); 
            ProtectedKey protectedKey = ProtectedKey.CreateFromPlaintextKey(key, DataProtectionScope.LocalMachine);
            IKeyWriter writer = new KeyReaderWriter();
            Stream keyOutput = File.Create(@"C:\mykey.key");
            writer.Write(keyOutput, protectedKey);

            //Hash Provider
            IDictionary<string, IHashProvider> hashProviders = new Dictionary<string, IHashProvider>();
            //Symmetric Provider
            IDictionary<string, ISymmetricCryptoProvider> symmetricProviders = new Dictionary<string, ISymmetricCryptoProvider>();
            ISymmetricCryptoProvider testProvider = new SymmetricAlgorithmProvider(Type.GetType(algorithm.GetType().AssemblyQualifiedName),
                protectedKey);

            symmetricProviders.Add(providerName, testProvider);
            CryptographyManagerImpl manager = new CryptographyManagerImpl(hashProviders, symmetricProviders);
            
            string encryptedText = manager.EncryptSymmetric(providerName, "MagicText");

            string decryptedText = manager.DecryptSymmetric(providerName, encryptedText);

 

NOTE: Creating the key programmatically was taken from this post: http://entlib.codeplex.com/Thread/View.aspx?ThreadId=62662

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com