Exception when using Fluent Interface for Cryptography

Topics: Cryptography Application Block
Jan 25, 2011 at 11:19 PM

I’m attempting to use the fluent interface to configure the Cryptography Block at run time using the following code.

Dim configBuilder As New ConfigurationSourceBuilder
Dim keyFile As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TestApp") & "\TestApp.key"
configBuilder.ConfigureCryptography.EncryptUsingSymmetricAlgorithmProviderNamed("RijndaelManaged").WithOptions.UseKeyFile(keyFile, DataProtectionScope.CurrentUser)
Dim configSource As New DictionaryConfigurationSource
configBuilder.UpdateConfigurationWithReplace(configSource)
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource)

Dim encryptedData As String = Cryptographer.EncryptSymmetric("RijndaelManaged", "Data To Encrypt")

However, I get the following exception when calling EncryptSymmetric().

 Message
{"Activation error occured while trying to get instance of type ISymmetricCryptoProvider, key "RijndaelManaged""}

Inner Exception
{"Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.ISymmetricCryptoProvider", name = "RijndaelManaged". Exception occurred while: Calling constructor Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider(System.Type algorithmType, System.String protectedKeyFileName, System.Security.Cryptography.DataProtectionScope protectedKeyProtectionScope, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Instrumentation.ISymmetricAlgorithmInstrumentationProvider instrumentationProvider). Exception is: ArgumentNullException - Value cannot be null. Parameter name: algorithmType ----------------------------------------------- At the time of the exception, the container was:    Resolving Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider,RijndaelManaged (mapped from Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.ISymmetricCryptoProvider, RijndaelManaged)   Calling constructor Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider(System.Type algorithmType, System.String protectedKeyFileName, System.Security.Cryptography.DataProtectionScope protectedKeyProtectionScope, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Instrumentation.ISymmetricAlgorithmInstrumentationProvider instrumentationProvider) "}

 

If I include the following in the app.config (in place of the fluent code above) everything works. However, hard coding the value for protectedKeyFilename won’t work in my situation. That’s why I’m looking to the fluent interface.

 <securityCryptographyConfiguration>
    <hashProviders>
      <add name="SHA1Managed" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.HashAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        algorithmType="System.Security.Cryptography.SHA1Managed, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        saltEnabled="true" />
    </hashProviders>
    <symmetricCryptoProviders>
      <add name="RijndaelManaged" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        algorithmType="System.Security.Cryptography.RijndaelManaged, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        protectedKeyFilename="C:\Documents and Settings\bbrown\Local Settings\Application Data\TestApp\TestApp.key"
        protectedKeyProtectionScope="CurrentUser" />
    </symmetricCryptoProviders>
  </securityCryptographyConfiguration>

Any thoughts?

Jan 26, 2011 at 1:21 AM

You missed specifying the type of SymmetricAlgorithm to use:

configBuilder.ConfigureCryptography() _
        .EncryptUsingSymmetricAlgorithmProviderNamed("RijndaelManaged") _
        .WithOptions.UsingSymmetricAlgorithm(Of RijndaelManaged) _
        .WithOptions.UseKeyFile(keyFile, DataProtectionScope.CurrentUser)

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 26, 2011 at 12:41 PM

Well, that's completely obvious now.  Thanks.