C++写的代码,懒得再用C#重写一次了,把C++封成DLL,结果发现比重写还要改的多,在此标记一下
预先配置一下:
右击工程 - 属性 - 配置属性 - C/C++ - 命令行
命令行增加:/D_CRT_SECURE_NO_WARNINGS
打开 项目 >>属性页 >>C/C++ >>预编译头,改为不使用预编译头
1. const char* 直接换成string
2. char*做形参或返回值,需要换成IntPtr
3. char*做形参并想要获取char*内容,使用ref IntPtr无用。只能将该char*改为返回值获得。
4. C库文件中需要定义为全局变量,编译成dll后才能被c#正常调用,否则指针地址有数据,指针内容看到的返回值为
C++
extern "C" __declspec(dllexport) int Add(int a, int b, char* pBuffer)
{
::MessageBoxA(NULL, pBuffer, pBuffer, MB_OK);
return a + b;
}
extern "C" __declspec(dllexport) int Aes_Decrypt256CBC(BYTE* inData, int inLen, BYTE* outData, BYTE* Key)
{
//encData=inData
//decData=outData
//rsa解密出的32字节数据 前16字节作为密钥,后16字节作为数据,进行加密
// unsigned char key[32] = { 0xB3,0xA7,0x2E,0x68,0x4E,0x77,0x1B,0xC0,0xA4,0xFE,0xE8,0x7C,0xA6,0xAD,0x60,0xC6,
// 0x79,0x9C,0x68,0x03,0x22,0x5B,0x1F,0x25,0xA8,0x93,0x24,0x0D,0x05,0x9A,0x34,0x20 };
//向量固定
unsigned char iv[16] = { 0xE8,0x1F,0xAB,0x16,0xC9,0x1C,0x4A,0x1F,0x97,0x6E,0xE6,0xBB,0x36,0xE0,0x7D,0x31 };//固定
int encLen = 0;
int outlen = 0;
int decLen = 0;
outlen = 0;
EVP_CIPHER_CTX* ctx;
ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit(ctx, EVP_aes_256_cbc(), Key, iv);
EVP_DecryptUpdate(ctx, outData, &outlen, inData, inLen);
decLen = outlen;
EVP_DecryptFinal(ctx, outData + outlen, &outlen);
decLen += outlen;
EVP_CIPHER_CTX_free(ctx);
outData[decLen] = '\0';
int cc = 0;
return decLen;
}
c#
[DllImport(@"AES_CBC_256_Decrypt.dll", EntryPoint = "Add", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int a, int b, string dbFile);
byte[] Thirdkey = { 0x1F, 0x02, 0x02, 0x3F, 0xB0, 0x7E, 0xDA, 0x6C, 0x4C, 0x2F, 0x3D, 0x26, 0xC4, 0x92, 0xE5, 0x81, 0x45, 0xFA, 0xF5, 0x5B, 0xF3, 0x53, 0xB7, 0x6A, 0x1E, 0xEB, 0xBC, 0x46, 0x84, 0x21, 0x91, 0xEA };
byte[] encrpt_data= {
0xcb,0x71,0xef,0x5e,0x43,0x65,0x18,0x06,0xb9,0xf6,0x1c,0xc3,0xd5,0x9a,0x9b,0x39,0x88,0x3d,0xae,0x18,0x71,0xfd,0x0f,0x2f,0x23,0xa8,0x7d,0x52,0x04,0xea,0x8b,0x85,0xc4,0x6c,0x76,0x65,0xb8,0x99,0x47,0x25,0x0c,0x1d,0x66,0x79,0x4a,0x6d,0xfc,0x10,0x6d,0x36,0xcd,0x18,0x91,0xca,0x22,0x22,0x05,0xd4,0x37,0xf4,0x28,0x81,0xd0,0xab,0xa2,0x53,0xf7,0x7b,0xae,0x8d,0x62,0xe1,0x63,0xe7,0xfb,0x5c,0x50,0x9e,0xa0,0xde,0xf2,0x09,0x66,0xcd,0x2b,0xce,0x7a,0x89,0x34,0x7b,0x58,0xa8,0x37,0xbc,0xec,0xe7,0xfc,0xc2,0x17,0xf6,0x5e,0xb2,0xb1,0xd4,0x28,0x51,0xef,0x6d,0x97,0x97,0x0a,0x19,0xcf,0xbd,0x83,0x7a,0x7a,0x83,0x3e,0x6d,0x20,0x9e,0x06,0x8f,0x09,0x62,0xe7,0x1c,0xfc,0xe1,0xb1,0x04,0xa4,0xbc,0xa6,0x70,0x3e,0xc3,0xbe,0x6c,0x34,0x26,0xb9,0x97,0x9b,0xa6,0x15,0x0b,0xd1,0x62,0x1a,0xc2,0x9d,0x75,0xe6,0x3f,0x5e,0xb9,0x17,0xd5,0x8a,0x14,0x40,0x0f,0x8b,0x6c,0x1f,0x9e,0x2c,0x40,0xed,0x62,0x43,0xff,0x73,0x27,0xdf,0xf5,0x62,0xa4,0xb4,0x94,0x53,0xbb,0xad,0xcd,0x5a,0x41,0xb1,0x1d,0xd4,0xb9,0xa8,0x7d,0x9e,0x92,0x43,0xc5,0x57,0x3a,0xb0,0xd2,0x7b,0xde,0x71,0x82,0x4c,0x24,0x45,0xf7,0xba,0x7a,0x8d,0xce,0x34,0x6f,0xac,0x0f,0xdf,0xa4,0xb1,0xab,0x73,0x8b
};
byte[] decrypt_result = new byte[1024];
int c =Aes_Decrypt256CBC( encrpt_data, encrpt_data.Length, decrypt_result, Thirdkey);
C++ 如果编译是x64,C#也要在生成中写成x64的