我有以下代码来加密 C++ DLL 中的字符串
EXPORT WCHAR* EncryptString(WCHAR* stringToEncrypt) {
aes_context ctx;
WCHAR* in = stringToEncrypt;
WCHAR* out;
WCHAR* key = L"TestKey";
BYTE* buffEnc = (BYTE*)malloc(16);
BYTE* keyBuffEnc = (BYTE*)malloc(32);
memset(buffEnc, 0, 16);
memset(keyBuffEnc, 0, 32);
memcpy(buffEnc, in, wcslen(in) * 2);
memcpy(keyBuffEnc, key, wcslen(key) * 2);
aes_set_key(&ctx, keyBuffEnc, 256);
aes_encrypt(&ctx, buffEnc, buffEnc);
out = (WCHAR*)buffEnc;
// free(buffEnc);
// free(keyBuffEnc);
return out;
}
我的问题是我无法释放分配的内存,否则结果会被破坏。我想知道如何在不丢失结果的情况下释放使用的内存?我要更改返回值的类型吗?
在此先感谢您的帮助。问候亨氏