CryptStringToBinary not working with a NULL terminated string. Why?
does anyone know why this code is not working?
#include "stdafx.h"
#include <windows.h>
#include <WinCrypt.h>
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t *bin = TEXT("ProductID:1233===>55555");
BYTE out2[1000];
DWORD olen;
olen = 1000;
if (CryptStringToBinary(bin, 0, 1, out2, &olen, 0, 0) == 0)
{
wprintf(TEXT("Failure\n"));
}
else
{
//wprintf(TEXT("rn%s\n"),out2);
wprintf(TEXT("Su开发者_StackOverflowccess\n"));
}
system("pause");
return 0;
}
Thank you very much in advance!
Tom
Because you specified a length (parameter 2) of 0?
Edit: Just to clarify our eventual solution in the comments below, the code in the original question (since edited) contained two errors:
- It was calling
CryptBinaryToString
instead ofCryptStringToBinary
. Since it's invalid to pass a 0 in the second parameter toCryptBinaryToString
, the function was failing. - It was passing 1 in the third parameter (dwFlags), which is interpreted as
CRYPT_STRING_BASE64
. Since the string to encrypt wasn't in base 64 (it contained invalid characters such as ':'), the function was failing. In general, passing a raw value instead of using an existing definition (e.g.,CRYPT_STRING_BASE64
) is not a good idea.
精彩评论