c++ - 多字节到宽字符转换后内存中存在临时拷贝

标签 c++ winapi unicode secure-coding

我正在尝试将安全字符字符串转换为 unicode 字符串。由于我需要处理安全数据,因此我确保在使用后清除内存。 我尝试按照 API 进行 unicode 转换。

  • MultiByteToWideChar()
  • A2W()
  • mbstowcs_s()

到 Unicode 字符串的转换在上述所有 API 中都成功运行。 经过上述操作后,我已经清除了所有安全数据内存,包括 o/p unicode 值。 但是在此之后转储内存时,unicode 字符串的拷贝仍保留在内存中。我已确保清除所有处理安全数据的变量(通过使用 SecureZeroMemory() API)。 我怀疑它是上述 API 使用的临时拷贝。我需要 unicode 值并且需要保护我的数据。我怎样才能做到这一点?

下面分享了代码片段。

CHAR* pszPassword = NULL;
UINT unPlainTextLen  = 0;

// Decrypt the secure data
if( DecryptSecureData( pszPassword, unPlainTextLen))
{
    WCHAR *ptcszPassword_o = new WCHAR[unPlainTextLen + 1];
    ptcszPassword_o[ unPlainTextLen ] = 0;
    size_t unSizeConverted = 0;
    if( 0 == mbstowcs_s( &unSizeConverted, ptcszPassword_o, unPlainTextLen + 1, 
                         reinterpret_cast<CHAR*>( pszPassword ), unPlainTextLen ))
    {   

       // Clear Decrypted o/p buffer
       SecureZeroMemory( pszPassword, unPlainTextLen);
       delete[] pszPassword;

       // Process unicode data ptcszPassword_o

       SecureZeroMemory( ptcszPassword_o, unPlainTextLen  * 2 );
       delete[] pszPassword;
    }              
}

最佳答案

考虑到它只是(多)字节宽,WCHAR lookup[256] 可能是一个可行的解决方案。您可以使用传递给 MultiByteToWideChar 的虚拟字符串来初始化该表 - 这不是安全漏洞。缺点:这不适用于实际的多字节编码。

关于c++ - 多字节到宽字符转换后内存中存在临时拷贝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27129143/

相关文章:

c++ - 节点代码或算法的通用树父

c++ - 如何声明指针成员禁止删除它

c++ - 如何使用 C++ 在 ActiveDirectory 中获取 maxpwdAge 属性值?

mysql - Latin1 数据库中的 UTF-8 数据 : can it be saved?

c++ - 包含一组自身和自定义比较器的类——循环引用

c++ - 使用递归 C 替换数组中的所有负值

c - 如果你释放一个不干净的设备上下文会发生什么?

winapi - Windows下如何用汇编语言编写hello world?

python - 我如何告诉 Python 2 中的 dict() 使用 unicode 而不是字节字符串?

html - 我正在尝试使用 Shruti 字体 (gujrati Unicode) 构建一个 HTML 文件,但它显示了一些随机符号