Difference in Windows Cryptography Service between

2019-08-23 10:17发布

I have a class of encryption that was developed in Microsoft Visual C++ 6.0, and it is working properly. I migrated the code to Microsoft Visual Studio 2008, this class is working, but with a different behavior.

When I pass a specific string for encryption, the encryption result generated in the compiled code in Visual C + + 6.0 is different than in Visual Studio 2008, and it should not happen.

Can anyone help me understand why this is happening?

Cryptography functions of the Windows Services used:

HCRYPTPROV hProv;
HCRYPTHASH hHash;
HCRYPTHASH hKey;
CString strDataKey;
DWORD dwSize;
DWORD dwSizeEncrypted;
BYTE * aszEncryptedBuffer;

.........(Other codes.....).......

CryptAcquireContext (& hProv, NULL, MS_STRONG_PROV, PROV_RSA_FULL, 0);

CryptCreateHash (hProv, CALG_MD5, 0, 0, & hHash);

CryptHashData (hHash, (const unsigned char *) strDataKey, dwSize, 0);

CryptDeriveKey (hProv, CALG_RC2, hHash, CRYPTDERIVEKEY_FLAG, & hKey);

aszEncryptedBuffer = new BYTE[dwSizeEncrypted];

memcpy(aszEncryptedBuffer,"Data to be encrypted",dwSize);

CryptEncrypt (hKey, 0, TRUE, 0, aszEncryptedBuffer, & dwSize, dwSizeEncrypted);

thanks in advance.

1条回答
看我几分像从前
2楼-- · 2019-08-23 10:59
CryptHashData (hHash, (const unsigned char *) strDataKey, dwSize, 0);

That's a killer cast you've got there. CString has a conversion operator for const TCHAR* but not for const unsigned char*. This tends to work by accident but you'll easily run out of luck. You cannot convert from one string representation to another with a cast. For example when the CString stores a Unicode string. It does with default VS2008 project settings. Project + Properties, General, Character Set option. Also consider wcstombs() or WideCharToMultiByte() to make a real conversion. The code page you convert to can and will affect the resulting string.

查看更多
登录 后发表回答