开发者

Trying to create a .NET DLL to be used with Non-.NET Application

I am trying to create a .NET DLL so I can use the cryptographic functions with my non .NET application.

I have created a class library so far with this code:

namespace AESEncryption
{
    public class EncryptDecrypt
    {
        private static readonly byte[] optionalEntropy = { 0x21, 0x05, 0x07, 0x08, 0x27, 0x02, 0x23, 0x36, 0x45, 0x50 };

        public interface IEncrypt
        {
            string Encrypt(string data, string filePath);
        };

        public class EncryptDecryptInt:IEncrypt
        {

            public string Encrypt(string data, string filePath)
            {
                byte[] plainKey;

                try
                {
                    // Read in the secret key from our cipher k开发者_如何学运维ey store
                    byte[] cipher = File.ReadAllBytes(filePath);
                    plainKey = ProtectedData.Unprotect(cipher, optionalEntropy, DataProtectionScope.CurrentUser);

                    // Convert our plaintext data into a byte array

                    byte[] plainTextBytes = Encoding.ASCII.GetBytes(data);

                    MemoryStream ms = new MemoryStream();

                    Rijndael alg = Rijndael.Create();

                    alg.Mode = CipherMode.CBC;
                    alg.Key = plainKey;
                    alg.IV = optionalEntropy;

                    CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

                    cs.Write(plainTextBytes, 0, plainTextBytes.Length);

                    cs.Close();

                    byte[] encryptedData = ms.ToArray();

                    return Convert.ToString(encryptedData);
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
    }
}

In my VC++ application, I am using the #import directive to import the TLB file created from the DLL, but the only available functions are _AESEncryption and LIB_AES etc

I don't see the interface or the function Encrypt.

When I try to instantiate so I can call the functions in my VC++ program, I use this code and get the following error:

HRESULT hr = CoInitialize(NULL);

IEncryptPtr pIEncrypt(__uuidof(EncryptDecryptInt));

error C2065: 'IEncryptPtr': undeclared identifier

error C2146: syntax error : missing ';' before identifier 'pIEncrypt'


You don't appear to have flagged the Interface as visible via COM. I'd expect to see something like:

namespace AESEncryption
{
    [Guid("[a new guid for the interface]")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IEncrypt        {
        string Encrypt(string data, string filePath);
    }

    [Guid("[a new guid for the class]")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class EncryptDecryptInt : IEncrypt
    {
        public string Encrypt(string data, string filePath)
        {
            // etc.
        }
    }
}


Without some extra work, C# .Net libraries require the host application to use the .net runtime environment.

Actually, here is an article describing how to call a .net dll from unmanaged code:

http://support.microsoft.com/kb/828736


Have a look at this question. The easiest option is to use Managed C++ to create a mixed mode DLL.

If you need an encryption library, why not use OpenSSL? It will give you better performance and less dependencies than relying on .NET.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜