CryptoAPI doesn't work and can't encrypt or decrypt a file

Hello

Here is my code...

When I encrypt a file then when I decrypt it, the file is changed and have more 7kb data.

What's wrong in my code?

If you think my code is wrong totally, please write a simple function that opens a file call CryptEncrypt with parameters and encrypts the file and saves it to output file.

My code seems to do all, but do nothing!
void initrc4()
{
 
	
	dwResult= 0;
	hProv = 0;
	hKey = 0;
	hSessionKey = 0;
	cbBlob = 0;
	pbBlob = NULL;
 
	
	cbBlob = file_length("C:\\Key.txt");
	pbBlob = (BYTE*)malloc(cbBlob + 1);
	memset(pbBlob, 0, cbBlob + 1); 
 
	FILE *fp = fopen("C:\\Key.txt", "rb");
	if (fp) {
		fread(pbBlob, 1, cbBlob, fp);
		fclose(fp);
	} else {
		free(pbBlob);
		pbBlob = NULL;
	}
 
	if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0))
	{
		dwResult = GetLastError();
		if (dwResult == NTE_BAD_KEYSET)
		{
			if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
			{
				dwResult = GetLastError();
 
				return;
			}
		} else {
			dwResult = GetLastError();
			return;
		}
	}
 
	if (pbBlob) {
		if (!CryptImportKey(hProv, pbBlob, cbBlob, 0, 0, &hSessionKey))
		{
			dwResult = GetLastError();
			return;
		}
	} else { 
		if (!CryptImportKey(hProv, PrivateKeyWithExponentOfOne, sizeof(PrivateKeyWithExponentOfOne), 0, 0, &hKey))
		{
			dwResult = GetLastError();
			return;
		}
 
		if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey))
		{
			dwResult = GetLastError();
 
			return;
		}	
	}
 
}
 
void Encrypt()
{
 
	initrc4();
	PBYTE pbBuffer = NULL;
	DWORD dwBlockLen;
	DWORD dwBufferLen;
	DWORD dwCount;
	int ENCRYPT_SUCCESS = 0;
	int iReturnCode = ENCRYPT_SUCCESS;
	BOOL bLastBlock = FALSE;
 
	FILE *hSource;
	FILE *f;
	f = fopen("C:\\Output.dll", "w+");
	hSource = fopen("C:\\TheDLL.dll", "rb");
 
	dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
 
	if(ENCRYPT_BLOCK_SIZE > 1)
		{
		dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
		}
	else
		{
		dwBufferLen = dwBlockLen;
		}
	pbBuffer = new BYTE [dwBufferLen + 1];
 
 
	do
		{
		dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
		if(ferror(hSource)) return;
 
 
 
		bLastBlock = (feof(hSource) ? TRUE : FALSE);
 
		CryptEncrypt(hSessionKey, 0, bLastBlock, 0, pbBuffer, &dwCount, dwBufferLen);
		CryptDecrypt(hSessionKey, 0, bLastBlock, 0, pbBuffer, &dwCount, dwBufferLen);
		
		fwrite(pbBuffer, 1, dwCount, f);
 
 
 
 
 
 
 
		} while(!feof(hSource));
 
		fclose(f);
		fclose(hSource);
 
}

Open in new window

LVL 17
CSecurityAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
>>         f = fopen("C:\\Output.dll", "w+");

You probably want to open the output file in binary mode too :

        f = fopen("C:\\Output.dll", "wb+");

or you might see some weird output (added characters like you noticed could be one result).



btw, why do you always allocate space for an extra byte which you never use ?

>>         pbBlob = (BYTE*)malloc(cbBlob + 1);

>>         pbBuffer = new BYTE [dwBufferLen + 1];

Also, it's not a good idea to mix C style memory allocation (malloc, free) with C++ style memory allocation (new, delete). Pick one and stick with it ;)
0
 
WerrockCommented:
I'm not at my work station so I cannot run it atm but here is a sample from MS that might help:
http://msdn.microsoft.com/en-us/library/ms925954.aspx
0
 
CSecurityAuthor Commented:
Thank you so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.