佚名通过本文主要向大家介绍了atl,atl71.dll,atl100.dll下载,atl71.dll修复,穿越火线atl71.dll等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: 如何使用ATL中的MD5加密类CCryptMD5Hash
描述:
解决方案1:
描述:
请提供示例代码,加密一个字符串
解决方案1:
#pragma once
#include <atlcrypt.h>
namespace VCUE
{
inline HRESULT EnsureAcquire(
CCryptProv& prov,
LPCTSTR pszContainer = NULL,
LPCTSTR pszProvider = MS_DEF_PROV,
DWORD dwProvType = PROV_RSA_FULL,
DWORD dwFlags = CRYPT_VERIFYCONTEXT | CRYPT_SILENT
)
{
HRESULT hr = prov.Initialize(dwProvType, pszContainer,
pszProvider, dwFlags);
if (hr == NTE_KEYSET_NOT_DEF)
hr = prov.Initialize(dwProvType, pszContainer, pszProvider,
dwFlags | CRYPT_NEWKEYSET);
return hr;
}
}
EnsureAcquire is a helper function that initializes a cryptographic provider (an object that provides cryptographic functions). The function provides sensible default arguments for use in server applications by setting the flag that disables any user interface that the cryptographic provider might display to the user (CRYPT_SILENT) and the flag that informs the provider that private keys do not need to be accessed (CRYPT_VERIFYCONTEXT). The function also ensures that if initialization fails because the container does not exist, an attempt is made to create it. For more information, see CCryptProv::Initialize. Add the following code to Encrypt.h:
inline HRESULT CreateSaltedHash(
CCryptProv& Provider,
const BYTE* Secret, DWORD SecretLength,
const BYTE* Salt, DWORD SaltLength,
BYTE* Hash, DWORD& HashLength
)
{
HRESULT hr = E_FAIL;
HCRYPTHASH hHash = 0;
if (CryptCreateHash(Provider.GetHandle(), CALG_MD5,
0, 0, &hHash))
{
CCryptHash oHash(hHash, TRUE);
hr = oHash.AddData(Secret, SecretLength);
if (SUCCEEDED(hr))
{
hr = oHash.AddData(Salt, SaltLength);
if (SUCCEEDED(hr))
{
DWORD Size = 0;
hr = oHash.GetSize(&Size);
if (SUCCEEDED(hr))
{
if (Size <= HashLength)
hr = oHash.GetValue(Hash, &HashLength);
else
hr = E_OUTOFMEMORY;
}
}
}
}
return hr;
}
CreateSaltedHash uses the MD5 hashing algorithm to generate a hash from a secret (such as a password) and a salt (a random number). The code creates a CCryptHash object, adds the secret and the salt to the hash, and then gets the value of the hash if the buffer provided by the caller is big enough to hold it. Add the following code to Encrypt.h:
inline HRESULT HashSecret(
const BYTE* Secret, DWORD SecretLength,
BYTE* Salt, DWORD& SaltLength,
BYTE* Hash, DWORD& HashLength
)
{
CCryptProv Provider;
HRESULT hr = EnsureAcquire(Provider);
if (SUCCEEDED(hr))
{
hr = Provider.GenRandom(SaltLength, Salt);
if (SUCCEEDED(hr))
{
&