i've got following code to encrypt a string in a 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;
}
My problem is that i can not free the allocated memory because otherwise the result is broken. I wonder how can i free the used memory without losing the result? Have i to change the type of return value?
Thanks in advance for your help. Greets Heinz