ConnectionString encryption decryption

From: Jase H (JaseH_at_discussions.microsoft.com)
Date: 03/17/05


Date: Wed, 16 Mar 2005 18:19:02 -0800

Hello,

I have a ASP.NET web application problem involving the data encryption and
decryption assembly(DLL) used on the connection string value that is set in
the webconfig file.
The problem occurs in the application when you instantiate a new instance of
the class as shown below:

---Dim dp As DPAPIComp.DataProtectorComp = New DPAPIComp.DataProtectorComp---
where DPAPIComp is the name of the namespace referenced to in the library
and DataProtectorComp is the class. This class contains the Encrypt and
Decrypt function used to encrypt and decrypt the connection string pass to
it.

Below is the code responsing to the call above. (in the form of an assembly)

using System;
using System.EnterpriseServices;
using System.Security.Principal;
using System.Runtime.InteropServices;
using DataProtection;

namespace DPAPIComp
{
        public class DataProtectorComp: ServicedComponent
        {
                public byte[] Encrypt(byte[] plainText)
                {
                        DataProtector dp = new DataProtector( DataProtector.Store.USE_USER_STORE );
                        byte[] cipherText = null;
                        try
                        {
                                cipherText = dp.Encrypt(plainText, null);
                        }
                        catch(Exception ex)
                        {
                                throw new Exception("Exception encrypting. " + ex.Message);
                        }
                        return cipherText;

                }
                public byte[] Decrypt(byte[] cipherText)
                {
                        DataProtector dp = new DataProtector( DataProtector.Store.USE_USER_STORE );
                        byte[] plainText = null;

                        try
                        {
                                plainText = dp.Decrypt(cipherText,null);
                        }
                        catch(Exception ex)
                        {
                                throw new Exception("Exception decrypting. " + ex.Message);
                        }
                        return plainText;
                }

                public DataProtectorComp()
                {

                }
        }
}
--------------------------------------------

The error message as it passes this line in the debugger is "Access is
denied".
Source: "mscorlib"
StackTrace: " at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode,
IntPtr errorInfo)
   at System.EnterpriseServices.Thunk.Proxy.CoCreateObject(Type serverType,
Boolean bQuerySCInfo, Boolean& bIsAnotherProcess, String& uri)
   at
System.EnterpriseServices.ServicedComponentProxyAttribute.CreateInstance(Type
serverType)
   at
System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(Type
serverType, Object[] props, Boolean bNewObj)
   at BoardroomBookings.DBConnection.GetConnectionString(String Name,
String& CnnStr) in C:\Inetpub\wwwroot\BoardroomBookings\DBConnection.vb:line
14" String

--------------------------------------------
I have tried placing the DLL in the GAC and giving it a strong name and that
still give the same error message. The DPAPI solution was built successfully.
Would this indicate that there is some sort of permission being applied to
the encryption/decryption call because I cannot connect to the server.
How do I get it to instantiate this probably and be able to connect to the
database.

Cheers,

Please note the same version works on the server but does not work on my
local workstation. Does this tell you something?



Relevant Pages

  • Re: Using encrypted dB connection string
    ... > Using 2-way to Encrypt and Decrypt and email forgotten passwords ... > connection string every single time you are going to connect to it. ... >> machine key for the actual machine on which you are running, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Use Dpapi with Shared Asp.Net Web Host?
    ... DPAPI is only suitable for encrypting and decrypting stuff on the same ... If you need to decrypt on a different machine, ... This article explains how to encrypt and store the connection string in the ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • RE: Encrypting/Decrypting Connection String
    ... User the DP API provided by Microsoft or use the .net crypto api which is ... that would encrypt this connection string, then copy and paste it in your ... then decrypt the connection string in the web.config file? ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Encrypting/Decrypting Connection String
    ... User the DP API provided by Microsoft or use the .net crypto api which is ... that would encrypt this connection string, then copy and paste it in your ... then decrypt the connection string in the web.config file? ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: NTE_BAD_DATA
    ... They are NOT used DIRECTLY to encrypt / decrypt data; ... you should generate a RANDOM SESSION KEY and select a SYMMETRIC ENCRYPTION ... // imported from a BLOB read in from the source file or having ...
    (microsoft.public.platformsdk.security)