Using Cryptography Application Block in SQL Server 2005

Topics: Cryptography Application Block, General discussion
Dec 14, 2009 at 11:37 PM


This is the SQL code to register MyAssembly in SQL Server 2005:

CREATE ASSEMBLY MyAssembly from <MyDLL location>

MyDLL references Cryptography Application Block.

Cryptography Application Block uses an App.config.

Is it possible to keep using App.config in SQL Server 2005? How?


Thanks in advance.


Merry Christmas! *<|:)











Dec 15, 2009 at 3:14 AM

Sorry but I'm not sure I understand your question.   Could you explain it a bit further?


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Dec 15, 2009 at 2:29 PM
Edited Dec 15, 2009 at 3:18 PM

Thank you Sarah

I am going to try ...

I must use a CLR function in order to create a Hash.

I'm trying to use an assembly called Cryptography that references the Cryptography Application Block.

There are several steps involved:

     * Create the CLR Dll for the SQL function to use.
           o This .NET assembly references  Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;  Microsoft.Practices.EnterpriseLibrary.Common;
     * Register the CLR Dll in SQL server
     * Create a normal SQL function that uses the CLR Dll

create assembly SQLServerEncryption from 'c:\Cryptography.dll' WITH PERMISSION_SET = UNSAFE


alter assembly SQLServerEncryption add file from 'c:\Cryptography.dll.config'

CREATE FUNCTION [dbo].[GETHASH]() RETURNS [nvarchar](255) WITH EXECUTE AS CALLER AS EXTERNAL NAME [SQLServerEncryption].[Cryptography].[GetHash]


select dbo.[GETHASH]( )

This last instruction returns the following error message:

Msg 6522, Level 16, State 2, Line 1

A .NET Framework error occurred during execution of user-defined routine or aggregate "GETHASH":

System.Configuration.ConfigurationErrorsException: The configuration section securityCryptographyConfiguration is not defined in the current configuration for the AppDomain.

System.Configuration.ConfigurationErrorsException: [...]


How to access the App.config that the Cryptography Application Block uses?


Any help will be appreciated.

Dec 16, 2009 at 7:05 AM


AFAIK, since it is a class library, it does not have a config, what you can do, is access the config file from the code inside your class library. You can do it by using the FileConfigurationSource of the Ent lib.

you can try something like this:

 IConfigurationSource source = new FileConfigurationSource(@"c:\crypto.config");
 SymmetricCryptoProviderFactory factory = new SymmetricCryptoProviderFactory(source);
 ISymmetricCryptoProvider provider = factory.Create("RijndaelManaged");

now you use the provider instance to encrypt and decrypt.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Dec 18, 2009 at 11:42 AM

Thank you Valiant.

The configuration file must be placed in 

C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn

otherwise access is denied.


It works! It works!


Merry Christmas!