|
From: <ric...@nt...> - 2004-04-18 18:42:20
|
I'm trying to encrypt a file and to be honest I don't have a clue how to do
it. SO I copied and pasted the example from the MSDN encryption example but
it keeps failing on this if statement:
// Get a handle to key exchange key.
if(!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey)) {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
goto done;
}
The error translates to key doesn't exist. OK but how do I create it? Or is
the example in the MSDN flawed somehow?
I've pasted my slightly modified code below. Any assitance would be
appreciated.
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include "resource.h"
extern void OutputErrorMessage();
extern void OutputDistinctErrorMessage(char *msg);
#define BLOCK_SIZE 160
#define BUFFER_SIZE (BLOCK_SIZE+16) // Give buffer 16 bytes of extra
// room for padding, and so forth.
BOOL SaveEncryptedTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
FILE *hDest = NULL;
int eof = 0;
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTKEY hXchgKey = 0;
BYTE pbBuffer[BUFFER_SIZE];
DWORD dwCount;
BYTE *pbKeyBlob = NULL;
DWORD dwBlobLen;
DWORD dwTextLength;
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hDest != INVALID_HANDLE_VALUE)
{
// Get a handle to the default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
OutputErrorMessage();
goto done;
}
// Create a random block cipher session key.
if(!CryptGenKey(hProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey)) {
OutputErrorMessage();
goto done;
}
// Get a handle to key exchange key.
if(!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey)) {
OutputErrorMessage();
goto done;
}
// Determine the size of the key blob and allocate memory.
if(!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, NULL, &dwBlobLen))
{
OutputErrorMessage();
goto done;
}
if((pbKeyBlob = (BYTE *) malloc(dwBlobLen)) == NULL) {
OutputErrorMessage();
goto done;
}
// Export the key into a simple key blob.
if(!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, pbKeyBlob,
&dwBlobLen)) {
OutputErrorMessage();
free(pbKeyBlob);
goto done;
}
// Write the size of the key blob to the destination file.
fwrite(&dwBlobLen, sizeof(DWORD), 1, hDest);
if(ferror(hDest)) {
OutputErrorMessage();
free(pbKeyBlob);
goto done;
}
// Write the key blob to the destination file.
fwrite(pbKeyBlob, 1, dwBlobLen, hDest);
if(ferror(hDest)) {
OutputErrorMessage();
free(pbKeyBlob);
goto done;
}
// Free memory.
free(pbKeyBlob);
dwTextLength = GetWindowTextLength(hEdit);
// No need to bother if there's no text.
if(dwTextLength > 0)
{
char * pszText;
DWORD dwBufferSize = dwTextLength + 1;
pszText = (char *) GlobalAlloc(GPTR, dwBufferSize);
if(pszText != NULL)
{
if(GetWindowText(hEdit, pszText, dwBufferSize))
{
DWORD dwWritten;
// Encrypt the data.
if(!CryptEncrypt(hKey, 0, eof, 0, pbBuffer, (DWORD *)
pszText,
dwBufferSize))
{
char *nmsg = "Error during CryptEncrypt!\n";
OutputDistinctErrorMessage(nmsg);
goto done;
}
// Write the data to the destination file.
fwrite(pbBuffer, 1, dwCount, hDest);
if(ferror(hDest)) {
printf("Error writing data!\n");
goto done;
}
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
bSuccess = TRUE;
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
done:
// Destroy the session key.
if(hKey != 0) CryptDestroyKey(hKey);
// Destroy the key exchange key.
if(hXchgKey != 0) CryptDestroyKey(hXchgKey);
// Release the provider handle.
if(hProv != 0) CryptReleaseContext(hProv, 0);
// Close the source file.
//if(hSource != NULL) fclose(hSource);
// Close destination file.
if(hDest != NULL) fclose(hDest);
return bSuccess;
}
-----------------------------------------
Email provided by http://www.ntlhome.com/
|