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

Posted by Changeling on Stack Overflow See other posts from Stack Overflow or by Changeling
Published on 2010-04-07T13:12:11Z Indexed on 2010/04/07 13:13 UTC
Read the original article Hit count: 233

Filed under:
|
|
|
|

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'

© Stack Overflow or respective owner

Related posts about c#

Related posts about c++