help implementing wincrypt..
hey, i'm trying to use wincrypt.h for some local encryption of plain text and for some reason its not working. its probably because i'm doing something wrong, lol.
does anyone know of a tutorial i could read?
here's the code i have so far:
Code:
void EncryptText(HWND hEdit)
{
//-------------------------------------------------------------------
// Declare and initialize variables.
HCRYPTPROV hCryptProv = 0; // handle for a cryptographic provider context
HCRYPTKEY hKey = 0; // handle for a cryptographic key
HCRYPTHASH hHash;
LPCSTR UserName = "MyKeyContainer"; // name of the key container to be used
DWORD dwDataSize;
TCHAR *szText;
//-------------------------------------------------------------------
// Get the text to be encrypted.
dwDataSize = GetWindowTextLength(hEdit);
if (dwDataSize > 0)
{
szText = malloc(dwDataSize+1);
GetWindowText(hEdit, szText, dwDataSize);
}
// Attempt to acquire a context and a key
// container. The context will use the default CSP
// for the RSA_FULL provider type. DwFlags is set to zero
// to attempt to open an existing key container.
if(CryptAcquireContext(
&hCryptProv, // handle to the CSP
UserName, // container name
NULL, // use the default provider
PROV_RSA_FULL, // provider type
0)) // flag values
{
MessageBox(NULL, UserName, "Cryptographic context key container", MB_OK);
}
else
{
//-------------------------------------------------------------------
// An error occurred in acquiring the context. This could mean
// that the key container requested does not exist. In this case,
// the function can be called again to attempt to create a new key
// container. Error codes are defined in Winerror.h.
if (GetLastError() == NTE_BAD_KEYSET)
{
if(CryptAcquireContext(
&hCryptProv,
UserName,
NULL,
PROV_RSA_FULL,
CRYPT_NEWKEYSET))
{
MessageBox(NULL, "A new key container has been created.", "ok", MB_OK);
}
else
{
MessageBox(NULL, "Could not create a new key container.", "OOPS", MB_OK);
exit(1);
}
}
else
{
MessageBox(NULL, "A cryptographic service handle could not be "
"acquired.", "damn", MB_OK);
exit(1);
}
} // End of else.
//-------------------------------------------------------------------
// A cryptographic context and a key container are available. Perform
// any functions that require a cryptographic provider handle.
// Create a random session key.
if(CryptGenKey(
hCryptProv,
KEYLENGTH | CALG_MD5,
CRYPT_EXPORTABLE,
&hKey))
{
MessageBox(NULL, "A session key has been created.", "SWEET", MB_OK);
}
else
{
MessageBox(NULL, "Error during CryptGenKey.", "........", MB_OK);
exit(1);
}
//-------------------------------------------------------------------
// The key created can be exported into a key BLOB that can be
// written to a file.
//--------------------------------------------------------------------
// Acquire a hash object handle.
if(CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
printf("An empty hash object has been created. \n");
}
else
{
printf("Error during CryptBeginHash!\n");
exit(1);
}
// Insert code that uses the hash object here.
/* Finally - on to the part where the text
* is actually encrypted.
*/
if (CryptEncrypt(hKey, hHash, TRUE, 0, szText, &dwDataSize, (DWORD)sizeof(dwDataSize)))
{
MessageBox(NULL, szText, "szText", MB_OK);
}
else
{
if (GetLastError() == ERROR_MORE_DATA)
{
MessageBox(NULL, "I got the ERROR_MORE_DATA error.. :(", "........!", MB_OK);
}
}
// ...
// When you have finished using the key, free the resource.
if (!CryptDestroyKey(hKey))
{
MessageBox(NULL, "Error during CryptDestroyKey.", "OOPS", MB_OK);
exit(1);
}
//--------------------------------------------------------------------
// After processing, hCryptProv and hHash must be released.
if(hHash)
CryptDestroyHash(hHash);
if(hCryptProv)
CryptReleaseContext(hCryptProv,0);
}
and that outputs that "Error during CryptGenKey"
?? :(
thanks
btw, that code is a compilation of the examples provided on the MSDN website for each function except for CryptEncrypt().