Crypt32函数CryptProtectData总是返回" \ x1"

时间:2017-12-26 17:05:21

标签: c crypt

尝试使用Crypt32函数CryptProtectData和CryptUnprotectData来加密和解密来自C程序的字符串。加密声称成功,但输出DATA_BLOB中的pbData始终只包含" \ x1"。以下是我目前用于加密的内容。关于我可能做错的任何建议?

这是在Windows 7上使用crypt32.dll v6.1.7601。

EXPORT short CALLBACK ASEncryptDPApiValue (char *pszEntropy, char *pszValue, char *pszEncryptValue, int iValueLen)
{
   short sRetCd = 0;
   char szDecryptValue [MAX_INI_VALUE] = "";
   DWORD dwLastErrorRC = 0;
   DWORD dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
   CHAR szMsgBuffer[200] = "";

   DATA_BLOB encrBlob;
   DATA_BLOB decrBlob = {0, 0};
   DATA_BLOB entropyBlob = {0, 0};
   LPWSTR pDescrOut =  NULL;

   if (pszValue[0] != '\0')
      {
      decrBlob.pbData = (BYTE *)pszValue;
      decrBlob.cbData = strlen((char *)decrBlob.pbData)+1;
      }
   // else return an error

   if (pszEntropy[0] != '\0')
      {
      entropyBlob.pbData = (BYTE *)pszEntropy;
      entropyBlob.cbData = strlen((char *)entropyBlob.pbData)+1;
      }

   if (CryptProtectData(&decrBlob,
                        pDescrOut,
                        &entropyBlob, 
                        NULL,
                        NULL,
                        dwFlags,
                        &encrBlob))
      {
      strcpy_s(pszEncryptValue, iValueLen, (char *) encrBlob.pbData);
      dwLastErrorRC = GetLastError();
      LocalFree(encrBlob.pbData);
      sprintf(szMsgBuffer, 
         _T("ASEncryptDPApiValue - Decrypting value - successful = %d  [%s]\r\n"), dwLastErrorRC, pszEncryptValue);
      OutputDebugString(szMsgBuffer);
      }
   else
      {
      sRetCd = TRUE;
      dwLastErrorRC = GetLastError();
      sprintf(szMsgBuffer, 
         _T("ASEncryptDPApiValue - error decrypting value - last OS error = %d\r\n"), dwLastErrorRC);
      OutputDebugString(szMsgBuffer);
      LocalFree(encrBlob.pbData);
      }

    return sRetCd;
}

0 个答案:

没有答案