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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Infinity08Commented:
>>         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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CSecurityAuthor Commented:
Thank you so much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.