views:

96

answers:

3

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 key 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'

+3  A: 

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

Kevin
But didn't microsoft make it backwards-compatible hence COM Compatibility?
0A0D
You're right, forgot about that way. Here is the article that shows you how to do it.
Kevin
+3  A: 

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.
        }
    }
}
Rowland Shaw
+2  A: 

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.

VladV
Or simply the CryptoAPI may be enough.
Matteo Italia