0

アプリケーションを作成しているときに、いくつかのSQLデータベースに接続し、データベースから詳細を取得します。

このアプリケーションでは、ユーザー名のパスワードなどのデータベース接続の詳細を暗号化する必要があります。はい、その非常に単純明快で単純な方法で、クレデンシャルを復号化するための方法を記述します。

しかし、私の場合、クレデンシャルを復号化するためにサードパーティの暗号化メカニズムに依存する必要があります。さらに、他の暗号化方式を使用するいくつかのSQLサーバーに接続する必要があります。したがって、暗号化アセンブリを動的にロードして暗号化メソッドを呼び出すようにアプリケーションをコーディングします。

しかし、アセンブリフォームAssembly.LoadFile( "Path")をロードすると、ロードされたアセンブリをアンロードできません。このアセンブリを別のアプリドメインにロードし、関連するメソッドを呼び出して、そのアプリドメインをアンロードしたと思います。この部分でいくつかの助けが必要です。知識が不足しているため、必要なメソッドを呼び出すことができません。私のコードは次のとおりです。これについて私を助けてください。

クラスApplicationSettings{

    private static ApplicationSettings m_ApplicationSettings;
    public String m_ServerName { get; private set; }
    public String m_DatabaseName { get; private set; }
    public String m_UserID { get; private set; }
    public String m_Password { get; private set; }
    public String m_EncryptionDLLPath{ get; private set; }
    public String m_NameSpace { get; private set; }
    public String m_ClassName { get; private set; }
    public String m_EncryptionMethodName { get; private set; }
    public String m_DecryptionMethodName { get; private set; }

    private ApplicationSettings()
    {
        m_ApplicationSettings = this;
    }

    public static ApplicationSettings CurrentValues
    {
        get
        {                
            return m_ApplicationSettings;
        }
        private set
        {
            m_ApplicationSettings = value;
        }
    }

    internal static void Initialize()
    {
        CommonFunctions.DataEncryption _enc = new CommonFunctions.DataEncryption();


        ApplicationSettings.CurrentValues = new ApplicationSettings();
        ApplicationSettings.CurrentValues.m_EncryptionDLLPath = @"C:\Users\Administrator\Documents\Visual Studio 2010\Projects\TestApp\TestApp\bin\Debug\AppSec.dll";
        ApplicationSettings.CurrentValues.m_NameSpace = "AppSec";
        ApplicationSettings.CurrentValues.m_ClassName = "AppSecEncDec";
        ApplicationSettings.CurrentValues.m_EncryptionMethodName = "Encrypt";
        ApplicationSettings.CurrentValues.m_DecryptionMethodName = "Decrypt";
        ApplicationSettings.CurrentValues.m_Password = _enc.Decrypt("pzBS3EJDoGM=");
        ApplicationSettings.CurrentValues.m_UserID = "sa";

    }



}

クラスDataEncryption{

    AppDomain DomainName;        

    //Call the Encryption Method 
    public String Encrypt(Object _DataToEncrypt)
    {


    }

    //Call the Decryption Method 
    public String Decrypt(Object _DataToDecrypt)
    {
        String _Decrypt = "";

        String assemblyFileName = ApplicationSettings.CurrentValues.m_EncryptionDLLPath;
        String assemblyName = ApplicationSettings.CurrentValues.m_NameSpace;

        //Setup the evidence
        Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
        AppDomain TestDomain = AppDomain.CreateDomain(
          "TestDomain", //The friendly name of the domain.
          evidence,   //Evidence mapped through the security policy to establish a top-of-stack permission set.
          AppDomain.CurrentDomain.BaseDirectory,  // The base directory that the assembly resolver uses to probe for assemblies.
          System.IO.Path.GetFullPath(assemblyFileName),    // The path relative to the base directory where the assembly resolver should probe for private assemblies.
          true  // If true, a shadow copy of an assembly is loaded into this application domain.
          );
        string s = TestDomain.Load(assemblyName).FullName;
        string[] myparam = new String[1];
        myparam[0] = "test";


        TestDomain.CreateInstance(TestDomain.Load(assemblyName).GetName().ToString(), ApplicationSettings.CurrentValues.m_NameSpace + "." + ApplicationSettings.CurrentValues.m_ClassName).CreateObjRef(GetType());
        //her i need to execute the Encrypt method which will load form the third party encryption mechanisam

        //method name will be returnd on this parameter in application settings Classes.ApplicationSettings.CurrentValues.m_EncryptionMethodName ;

        UloadAssembly();

        return _Decrypt;
    }


    public void UloadAssembly()
    {
        //Unload the loaded appdomain
        AppDomain.Unload(DomainName);            
        GC.Collect();
    }


}

前もって感謝します。

4

1 に答える 1

0

私はこれを行う方法を理解しました、そしてそれが完全に成功することを願っています以下のコードを見つけてください。

       public String Encrypt(Object _DataToEncrypt)
    {
        try
        {

            String _Encrypt = "";
            LoadAssembly();
            ShowLoadedAssemblies();
            if (ClassInstance != null)
            {
                MethodInfo EncryptionMethod = ClassInstance.GetType().GetMethod(Classes.ApplicationSettings.CurrentValues.m_EncryptionMethodName); ;
                if (EncryptionMethod != null)
                {
                    object[] myparam = new object[1];
                    myparam[0] = _DataToEncrypt;
                    _Encrypt = (string)EncryptionMethod.Invoke(null, myparam);
                }

            }

            UloadAssembly();
            ShowLoadedAssemblies();
            return _Encrypt;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);


        }

    }

    //Call the Decryption Method 
    public String Decrypt(Object _DataToDecrypt)
    {
        String _Decrypt = "";

        LoadAssembly();
        ShowLoadedAssemblies();
        if (ClassInstance != null)
        {
            MethodInfo DecryptionMethod = ClassInstance.GetType().GetMethod(Classes.ApplicationSettings.CurrentValues.m_DecryptionMethodName);;
            if (DecryptionMethod != null)
            {
                object[] myparam = new object[1];
                myparam[0] = _DataToDecrypt;
                _Decrypt = (string)DecryptionMethod.Invoke(null, myparam);
            }               

        }
        UloadAssembly();
        ShowLoadedAssemblies();
        return _Decrypt;
    }
    //Loading the Assembly 
    public void LoadAssembly()
    {




        Evidence evi = new Evidence(AppDomain.CurrentDomain.Evidence);

        DomainName = AppDomain.CreateDomain(Classes.ApplicationSettings.CurrentValues.m_NameSpace
                                            , evi
                                            , AppDomain.CurrentDomain.BaseDirectory
                                            , Classes.ApplicationSettings.CurrentValues.m_EncryptionDLLPath
                                            , true
                                            );

       String LoadingAssemblyName = AssemblyName.GetAssemblyName(Classes.ApplicationSettings.CurrentValues.m_EncryptionDLLPath).FullName;

       ClassInstance = DomainName.CreateInstanceAndUnwrap(LoadingAssemblyName
                                                           , Classes.ApplicationSettings.CurrentValues.m_NameSpace 
                                                              + "." 
                                                              + Classes.ApplicationSettings.CurrentValues.m_ClassName
                                                           );

    }
    public void UloadAssembly()
    {
        //Unload the loaded appdomain
        AppDomain.Unload(DomainName);            
        GC.Collect();
    }
于 2011-07-09T13:00:43.230 に答える