Question

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("Success\n"));
}
system("pause");
    return 0;
}

Thank you very much in advance!

Tom

Was it helpful?

Solution

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:

  1. It was calling CryptBinaryToString instead of CryptStringToBinary. Since it's invalid to pass a 0 in the second parameter to CryptBinaryToString, the function was failing.
  2. 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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top