开发者

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:

  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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜