WEB开发网
开发学院软件开发VC 使用Visual C++创建Crypto加/解密组件 阅读

使用Visual C++创建Crypto加/解密组件

 2008-11-13 19:30:27 来源:WEB开发网   
核心提示: 现在,当接收一个数组参数作为变量,使用Visual C++创建Crypto加/解密组件(5),该数组可能藏在结构中的某个地方,所以需要一个判断嵌套, // Decrypt the Data.if ( ! CryptDecrypt(hKey, 0, true, 0, pbData, &dwC

现在,当接收一个数组参数作为变量,该数组可能藏在结构中的某个地方,所以需要一个判断嵌套。

//Get the safe array out of the Variant.
  if (vCipherText.vt == (VT_VARIANT | VT_BYREF))
  {
  if (vCipherText.pvarVal->vt == (VT_ARRAY | VT_VARIANT))
  SafeArrayCopy(vCipherText.pvarVal->parray, &psa);
  else
  {
  if (vCipherText.pvarVal->vt == (VT_ARRAY | VT_VARIANT | VT_BYREF))
  SafeArrayCopy(*(vCipherText.pvarVal->pparray), &psa);
  }
  }
  else
  {
  if (vCipherText.vt == (VT_ARRAY | VT_VARIANT | VT_BYREF))
  SafeArrayCopy(*(vCipherText.pparray), &psa);
  else
  {
  if (vCipherText.vt == (VT_ARRAY | VT_VARIANT))
  SafeArrayCopy(vCipherText.parray, &psa);
  else
  return DISP_E_TYPEMISMATCH;
  }
  }

需要密文和密码都是BYTE*类型。

//Convert the SAFEARRAY into a form we can use.
  SafeArrayAccessData(psa, (void HUGEP* FAR*)&pVar);
  SafeArrayGetLBound(psa, 1, &lBound);
  SafeArrayGetUBound(psa, 1, &uBound);
  dwOffset = 0 - lBound;
  dwCryptDataLen = uBound + dwOffset + 1;
  //Allocate memory
  pbData = (BYTE *)malloc(dwCryptDataLen);
  //Copy the array
  for(DWORD i = lBound; i <= uBound; i++){ if( ! (pVar[i].vt & VT_UI1)){ //Data Elements must be VT_UI1 (Bytes). free(pbData); return DISP_E_TYPEMISMATCH; } pbData[i + dwOffset]="pVar[i].uiVal;" } //Get Password pbPassword="(BYTE*)OLE2A(bstrPassword);</PRE">
  取得 Crypto Provider 的句柄。 // Get handle to the default provider.
  if (! CryptAcquireContext(&hProv, "aspZoneCryptoComponent",
  MS_DEF_PROV, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET))
  {
  if (! CryptAcquireContext(&hProv, "aspZoneCryptoComponent",
  MS_DEF_PROV, PROV_RSA_FULL, (CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET)))
  {
  dwError = GetLastError();
  sprintf(buffer, "Error %x during CryptAcquireContext", dwError);
  return Error(buffer);
  }
  }
  从 password 中得到 session key. // Create a hash object.
  if ( ! CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
  dwError = GetLastError();
  sprintf(buffer, "Error %x during CryptCreateHash", dwError);
  return Error(buffer);
  }
  // Hash in the password.
  if ( ! CryptHashData(hHash, pbPassword, SysStringLen(bstrPassword), 0)) {
  dwError = GetLastError();
  sprintf(buffer, "Error %x during CryptHashData", dwError);
  return Error(buffer);
  }
  // Derive a session key from the hash object.
  if ( ! CryptDeriveKey(hProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey)) {
  dwError = GetLastError();
  sprintf(buffer, "Error %x during CryptDeriveKey", dwError);
  return Error(buffer);
  }
  // Destroy hash object.
  CryptDestroyHash(hHash);
  hHash = 0;

将密文解密到纯文本中。

// Decrypt the Data.
  if ( ! CryptDecrypt(hKey, 0, true, 0, pbData, &dwCryptDataLen)) {
  dwError = GetLastError();
  sprintf(buffer, "Error %x during CryptDecrypt", dwError);
  return Error(buffer);
  }
  //Terminate the string with a null
  pbData[dwCryptDataLen] = NULL;

设置返回值,大扫除,然后搞定。

//Place Decrypted data into retval
  *bstrPlainText = SysAllocString(A2OLE((const char *)pbData));
  // Destroy session key.
  if(hKey) CryptDestroyKey(hKey);
  // Release provider handle.
  if(hProv) CryptReleaseContext(hProv, 0);
  return S_OK;
  }

上一页  1 2 3 4 5 

Tags:使用 Visual 创建

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接