当我在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))
答案 0 :(得分:0)
这是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)