base64加解密算法调用C++ DLL报错
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
C++
DLL_EXPORT char* _stdcall encodes(const char *src)
{
int i = 0, j = 0;
int dst_len = 1024*1024;
char *dst = new char[dst_len];
int src_len = strlen(src);
char base64_map[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if (src == NULL || src_len == 0 || dst == NULL || dst_len == 0)
{
return NULL;
}
int len = ((src_len + 2) / 3) << 2;
if (dst_len < (len + 1))
{
return NULL;
}
memset(dst, 0, dst_len); // 修改了数组越界的问题
int count = src_len - src_len % 3;
for (; i < count; i += 3)
{
dst[j++] = base64_map[(src[i] >> 2) & 0x3F];
dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];
dst[j++] = base64_map[((src[i + 1] << 2) & 0x3C) + ((src[i + 2] >> 6) & 0x3)];
dst[j++] = base64_map[src[i + 2] & 0x3F];
}
if (src_len % 3 == 1)
{
dst[j++] = base64_map[(src[i] >> 2) & 0x3F];
dst[j++] = base64_map[(src[i] << 4) & 0x30];
dst[j++] = '=';
dst[j++] = '=';
}
else if (src_len % 3 == 2)
{
dst[j++] = base64_map[(src[i] >> 2) & 0x3F];
dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];
dst[j++] = base64_map[(src[i + 1] << 2) & 0x3C];
dst[j++] = '=';
}
dst_len = j;
return _strdup(dst);
}
string ret = "武汉理工大学";
string retValue = "";