使用Visual C++创建Crypto加/解密组件
2008-11-13 19:30:27 来源:WEB开发网现在,当接收一个数组参数作为变量,该数组可能藏在结构中的某个地方,所以需要一个判断嵌套。
//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;
}
- ››使用linux中的quota教程
- ››使用jxl生成带动态折线图的excel
- ››创建SQL2005自动备份,定期删除的维护计划
- ››使用mysql mysqldump进行数据库迁移
- ››Visual Basic 2008 数学函数
- ››使用jquery是新tab形式
- ››使用QUnit进行Javascript单元测试
- ››使用UITextFieldDelegate来隐藏键盘
- ››使用公式提取Excel中的日期后发现格式不对
- ››Visual Studio2005中Smart Device的问题
- ››使用SQL Azure 的BI 解决方案
- ››使用PLSQL Developer工具导出sql文件
更多精彩
赞助商链接