CryptEncrypt为不同的语言提供不同的结果

CryptEncrypt gives different results for different languages

本文关键字:结果 语言 CryptEncrypt      更新时间:2023-10-16

当我在C++(如图所示)、C#或VB6中运行此代码时,我在CryptEncrypt中得到了不同的pbBuffer结果!我唯一能想到的是CryptEncrypt是基于语言的,或者其他函数的参数是错误的。发生这种情况的其他原因是什么?我是否向CryptDeriveKey传递了错误的参数?

if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))     
{   
    if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))        
    {   
        if (CryptHashData(hHash, (BYTE *)szLocalPassword, _tcslen(szLocalPassword), 0))            
        {
            if (CryptDeriveKey(hProv, CALG_RC4, hHash, CRYPT_EXPORTABLE, &hKey))                   
            {   
                dwLength = sizeof(TCHAR)*_tcslen(sUnencryptedString);                      
                BYTE *pbBuffer = (BYTE *)malloc(dwLength);                     
                if (pbBuffer != NULL)                      
                {   
                // Convert TCHAR to BYTE
                char c_szText[128] = {0};       // Any char
                SIZE_T so = wcslen(sUnencryptedString) + 1;
                wcstombs(c_szText, sUnencryptedString, so);
                memcpy(pbBuffer, c_szText, dwLength);
                if (CryptEncrypt(hKey, 0, TRUE, 0, pbBuffer, &dwLength, dwLength))                         

这是VB代码,展示了如何将正确的信息传递给Crypto API,以在VB6中加密字符串。希望有人能在搜索时使用这些信息!

...
Dim bKeyRoot() As Byte
bKeyRoot = StrConv(sKeyRoot, vbFromUnicode)
lResult = CryptHashData(hHash, bKeyRoot(0), Len(sKeyRoot), 0)
...
Dim bText() As Byte
bText = StrConv(sText, vbFromUnicode)
lResult = CryptEncrypt(lKey, 0, 1, 0, bText(0), lClear, lData)
sText = StrConv(bText, vbUnicode)