2

我正在尝试将安全的 char 字符串转换为 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;
    }              
}
4

2 回答 2

0

我有一个解决方案可以在没有临时副本的情况下转换为宽字符。

CHAR* pszSecuredData;
// Holds the secured multibyte data in pszSecuredData.

WCHAR* ptcszSecureData = new wchar_t[unSecureDataLength + 1];
swprintf( ptcszSecureData, unSecureDataLength + 1, L"%S", pszSecuredData );
于 2014-11-27T04:22:48.257 回答
0

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

于 2014-11-25T14:43:36.260 回答