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

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

 2008-11-13 19:30:27 来源:WEB开发网   
核心提示: // Place Encrypted Data into a VARIANT SAFEARRAY of VARIANT BYTESAFEARRAYBOUND rgsabound[] = {dwCryptDataLen, 0};psa = SafeArrayCreate(VT_VARIANT

// Place Encrypted Data into a VARIANT SAFEARRAY of VARIANT BYTE
  SAFEARRAYBOUND rgsabound[] = {dwCryptDataLen, 0};
  psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
  VARIANT* rgElems;
  SafeArrayAccessData(psa, (LPVOID*)&rgElems);
  for(DWORD i=0;i<dwCryptDataLen;i++){
  VariantInit(&rgElems[i]);
  rgElems[i].vt = VT_UI1;
  rgElems[i].uiVal = pbData[i];
  }
  SafeArrayUnaccessData(psa);
  VariantInit(vCipherText);
  vCipherText->vt = (VT_ARRAY | VT_VARIANT) ;
  vCipherText->parray = psa;

稍微整理一下,搞定。

// Destroy session key.
  if(hKey) CryptDestroyKey(hKey);
  // Release provider handle.
  if(hProv) CryptReleaseContext(hProv, 0);
  return S_OK;

实现解密方法

首先定义变量。

STDMETHODIMP CCrypto::Decrypt(VARIANT vCipherText,
  BSTR bstrPassword,
  BSTR *bstrPlainText)
  {
  HCRYPTPROV hProv = 0;
  HCRYPTHASH hHash = 0;
  HCRYPTKEY hKey = 0;
  SAFEARRAY* psa;
  VARIANT HUGEP *pVar;
  BYTE *pbData;
  BYTE *pbPassword;
  long lBound, uBound;
  DWORD dwCryptDataLen = 0;
  DWORD dwOffset = 0;
  DWORD dwError = 0;
  char buffer[200];
  USES_CONVERSION;

同样的原因,我们要调用RevertToSelf() RevertToSelf();

上一页  1 2 3 4 5  下一页

Tags:使用 Visual 创建

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