本文整理汇总了C++中MemoryBuffer::Write方法的典型用法代码示例。如果您正苦于以下问题:C++ MemoryBuffer::Write方法的具体用法?C++ MemoryBuffer::Write怎么用?C++ MemoryBuffer::Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemoryBuffer
的用法示例。
在下文中一共展示了MemoryBuffer::Write方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: EncryptBuffer
bool StandardEncryption::EncryptBuffer (MemoryBuffer *memSource, PCHAR szPassword, bool bEncrypt)
{
HCRYPTPROV hCryptProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;
DWORD dwBufferlen;
DWORD dwBufsize;
MemoryBuffer memOutput;
// Get the handle to the default provider.
if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROVIDER , 0)) {
//printf("A cryptographic provider has been acquired. \n");
} else {
MyHandleError("Error during CryptAcquireContext!");
return false;
}
if(!szPassword ) {
return false;
} else {
// Create a hash object.
if(CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)) {
//printf("A hash object has been created. \n");
} else {
MyHandleError("Error during CryptCreateHash!");
return false;
}
//-------------------------------------------------------------------
// Hash the password.
if(CryptHashData(hHash, (BYTE *)szPassword, strlen(szPassword), 0)) {
//printf("The password has been added to the hash. \n");
} else {
MyHandleError("Error during CryptHashData.");
return false;
}
//-------------------------------------------------------------------
// Derive a session key from the hash object.
if(CryptDeriveKey(hCryptProv, m_Currentalg, hHash, m_dwKeylength, &hKey)) {
//printf("An encryption key is derived from the password hash. \n");
} else {
MyHandleError("Error during CryptDeriveKey!");
return false;
}
//-------------------------------------------------------------------
// Destroy hash object.
if(hHash) {
if(!(CryptDestroyHash(hHash))) {
MyHandleError("Error during CryptDestroyHash");
return false;
}
hHash = 0;
}
// Encrypt / Decrypt data.
if (bEncrypt == true) {
// First get the size of the buffer needed.
dwBufferlen = memSource->GetSize ();
dwBufsize = memSource->GetSize ();
CryptEncrypt (hKey, 0, TRUE, 0, NULL, &dwBufferlen, dwBufsize);
if (dwBufferlen > 0) {
dwBufsize = dwBufferlen;
memOutput.SetSize (dwBufferlen);
memOutput.Write (memSource->GetBuffer (), 0, memSource->GetSize ());
if (!CryptEncrypt (hKey, 0, FALSE, 0, (BYTE *) memOutput.GetBuffer (), &dwBufferlen, dwBufsize)) {
MyHandleError ("Error during Encrypt.");
return false;
} else {
memSource->Clear ();
memSource->SetSize (memOutput.GetSize ());
memSource->Write (memOutput.GetBuffer (), 0, memOutput.GetSize ());
memOutput.Clear ();
}
} else {
OutputText ("Unable to obtain encrypted buffer size.");
return false;
}
} else {
dwBufferlen = memSource->GetSize ();
memOutput.SetSize (dwBufferlen);
memOutput.Write (memSource->GetBuffer (), 0, memSource->GetSize ());
if (!CryptDecrypt (hKey, 0, FALSE, 0, (BYTE *) memOutput.GetBuffer (), &dwBufferlen)) {
MyHandleError ("Error during Decrypt.");
return false;
} else {
memSource->Clear ();
memSource->SetSize (dwBufferlen);
memSource->Write (memOutput.GetBuffer (), 0, dwBufferlen);
memOutput.Clear ();
}
}
//-------------------------------------------------------------------
// Destroy the session key.
//.........这里部分代码省略.........