VC++ 利用CryptoAPI加密和解密算法的实现

接口声明:

	// 加密解密数据
	BOOL xEncrypt(BYTE *pbSourceData, DWORD dwSourceDataLen, BYTE **pbDestData, DWORD *pdwDestDataLen, LPCTSTR lpszPassword);

	// 加密文件
	BOOL xEncrypt(LPCTSTR lpszSourceFile, LPCTSTR lpszDestinationFile, LPCTSTR lpszPassword);

	// 解密数据
	BOOL xDecrypt(BYTE *pbSourceData, DWORD dwSourceDataLen, BYTE **pbDestData, DWORD *pdwDestDataLen, LPCTSTR lpszPassword);

	// 解密文件
	BOOL xDecrypt(LPCTSTR lpszSourceFile, LPCTSTR lpszDestinationFile, LPCTSTR lpszPassword);

源码实现:

BOOL Encrypt(BYTE *pbSourceData, DWORD dwSourceDataLen, BYTE **pbDestData, DWORD *pdwDestDataLen, LPCTSTR lpszPassword)
{
	HCRYPTPROV hProv = 0;
	HCRYPTHASH hHash = 0;
	HCRYPTKEY hKey = 0, hXchgKey = 0;
	PBYTE pbBuffer = NULL, pbKeyBlob = NULL;
	BOOL bEOF = FALSE, bReturn = FALSE;
	DWORD dwCount = 0, dwKeyBlobLen = 0;

	if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
	{
		DWORD ret = GetLastError();
		if (ret != NTE_BAD_KEYSET)
		{
			goto exit;
		}
		else
		{
			if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
			{
				goto exit;
			}
		}
	}

	if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
	{
		goto exit;
	}

	if (!CryptHashData(hHash, (PBYTE)lpszPassword, _tcslen(lpszPassword), 0))
	{
		goto exit;
	}

	if (!CryptDeriveKey(hProv, CALG_RC2, hHash, 0, &hKey))
	{
		goto exit;
	}

	if ((pbBuffer = (PBYTE)malloc(BUFFER_SIZE)) == NULL)
	{
		goto exit;
	}

	memset(pbBuffer, 0, BUFFER_SIZE);
	memcpy(pbBuffer, pbSourceData, dwSourceDataLen);
	dwCount = dwSourceDataLen;
	bEOF = TRUE;

	if (!CryptEncrypt(hKey, 0, bEOF, 0, pbBuffer, &dwCount, BUFFER_SIZE))
	{
		goto exit;
	}

	*pbDestData = (PBYTE)malloc(dwCount);
	memcpy(*pbDestData, pbBuffer, dwCount);
	*pdwDestDataLen = dwCount;
	bReturn = TRUE;

exit:
	if (pbKeyBlob) free(pbKeyBlob);
	if (hKey) CryptDestroyKey(hKey);
	if (pbBuffer) free(pbBuffer);
	if (hXchgKey) CryptDestroyKey(hXchgKey);
	if (hHash) CryptDestroyHash(hHash);
	if (hProv) CryptReleaseContext(hProv, 0);

	return bReturn;
}

// 解密数据
BOOL Decrypt(BYTE *pbSourceData, DWORD dwSourceDataLen, BYTE **pbDestData, DWORD *pdwDestDataLen, LPCTSTR lpszPassword)
{
	HCRYPTPROV hProv = 0;
	HCRYPTHASH hHash = 0;
	HCRYPTKEY hKey = 0;

	PBYTE pbBuffer = NULL, pbKeyBlob = NULL;
	BOOL bEOF = 0, bReturn = FALSE;
	DWORD dwCount = 0, dwKeyBlobLen = 0;

	if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
	{
		DWORD ret = GetLastError();
		if (ret != NTE_BAD_KEYSET)
		{
			goto exit;
		}
		else
		{
			if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
			{
				goto exit;
			}
		}
	}


	if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
	{
		goto exit;
	}

	if (!CryptHashData(hHash, (PBYTE)lpszPassword, _tcslen(lpszPassword), 0))
	{
		goto exit;
	}

	if (!CryptDeriveKey(hProv, CALG_RC2, hHash, 0, &hKey))
	{
		goto exit;
	}

	if ((pbBuffer = (PBYTE)malloc(BUFFER_SIZE)) == NULL)
	{
		goto exit;
	}

	memset(pbBuffer, 0, BUFFER_SIZE);
	memcpy(pbBuffer, pbSourceData, dwSourceDataLen);
	dwCount = dwSourceDataLen;
	bEOF = TRUE;

	if (!CryptDecrypt(hKey, 0, bEOF, 0, pbBuffer, &dwCount))
	{
		TRACE(_T("GetLastError()=%d"),GetLastError());
		goto exit;
	}

	*pbDestData = (PBYTE)malloc(dwCount);
	memcpy(*pbDestData, pbBuffer, dwCount);
	*pdwDestDataLen = dwCount;
	bReturn = TRUE;

exit:
	if (pbKeyBlob) free(pbKeyBlob);
	if (hKey) CryptDestroyKey(hKey);
	if (pbBuffer) free(pbBuffer);
	if (hHash) CryptDestroyHash(hHash);
	if (hProv) CryptReleaseContext(hProv, 0);

	return bReturn;
}


// 加密文件
BOOL Encrypt(LPCTSTR lpszSourceFile, LPCTSTR lpszDestinationFile, LPCTSTR lpszPassword)
{
	FILE *hSrcFile = NULL, *hDestFile = NULL;

	HCRYPTPROV hProv = 0;
	HCRYPTHASH hHash = 0;
	HCRYPTKEY hKey = 0, hXchgKey = 0;
	PBYTE pbBuffer = NULL, pbKeyBlob = NULL;
	BOOL bEOF = FALSE, bReturn = FALSE;
	DWORD dwCount = 0, dwKeyBlobLen = 0;

	if (fopen_s(&hSrcFile, lpszSourceFile, _T("rb")) != 0)
	{
		goto exit;
	}

	if (fopen_s(&hDestFile, lpszDestinationFile, _T("wb")) != 0)
	{
		goto exit;
	}

	if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
	{
		DWORD ret = GetLastError();
		if (ret != NTE_BAD_KEYSET)
		{
			goto exit;
		}
		else
		{
			if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
			{
				goto exit;
			}
		}
	}

	if (lpszPassword == NULL)
	{
		if (!CryptGenKey(hProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey))
		{
			goto exit;
		}

		if (!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey))
		{
			DWORD ret = GetLastError();
			if (ret != NTE_NO_KEY)
			{
				goto exit;
			}
			if (!CryptGenKey(hProv, AT_KEYEXCHANGE, NULL, &hXchgKey))
			{
				goto exit;
			}
		}

		if (!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, NULL, &dwKeyBlobLen))
		{
			goto exit;
		}

		if ((pbKeyBlob = (PBYTE)malloc(dwKeyBlobLen)) == NULL)
		{
			goto exit;
		}

		if (!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, pbKeyBlob, &dwKeyBlobLen))
		{
			goto exit;
		}

		fwrite(&dwKeyBlobLen, sizeof(DWORD), 1, hDestFile);

		if (ferror(hDestFile))
		{
			goto exit;
		}

		fwrite(pbKeyBlob, 1, dwKeyBlobLen, hDestFile);

		if (ferror(hDestFile))
		{
			goto exit;
		}
	}
	else
	{
		if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
		{
			goto exit;
		}

		if (!CryptHashData(hHash, (PBYTE)lpszPassword, _tcslen(lpszPassword), 0))
		{
			goto exit;
		}

		if (!CryptDeriveKey(hProv, CALG_RC2, hHash, 0, &hKey))
		{
			goto exit;
		}
	}

	if ((pbBuffer = (PBYTE)malloc(BUFFER_SIZE)) == NULL)
	{
		goto exit;
	}

	do
	{
		dwCount = fread(pbBuffer, 1, BLOCK_SIZE, hSrcFile);

		if (ferror(hSrcFile))
		{
			goto exit;
		}
		bEOF = feof(hSrcFile);

		if (!CryptEncrypt(hKey, 0, bEOF, 0, pbBuffer, &dwCount, BUFFER_SIZE))
		{
			goto exit;
		}

		fwrite(pbBuffer, 1, dwCount, hDestFile);
		if (ferror(hDestFile))
		{
			goto exit;
		}

	} while (!bEOF);

	bReturn = TRUE;

exit:
	if (hSrcFile) fclose(hSrcFile);
	if (hDestFile) fclose(hDestFile);
	if (pbKeyBlob) free(pbKeyBlob);
	if (pbBuffer) free(pbBuffer);
	if (hKey) CryptDestroyKey(hKey);
	if (hXchgKey) CryptDestroyKey(hXchgKey);
	if (hHash) CryptDestroyHash(hHash);
	if (hProv) CryptReleaseContext(hProv, 0);

	return bReturn;
}

// 解密文件
BOOL Decrypt(LPCTSTR lpszSourceFile, LPCTSTR lpszDestinationFile, LPCTSTR lpszPassword)
{
	FILE *hSrcFile = NULL, *hDestFile = NULL;

	HCRYPTPROV hProv = 0;
	HCRYPTHASH hHash = 0;
	HCRYPTKEY hKey = 0;

	PBYTE pbBuffer = NULL, pbKeyBlob = NULL;
	BOOL bEOF = 0, bReturn = FALSE;
	DWORD dwCount = 0, dwKeyBlobLen = 0;

	if (fopen_s(&hSrcFile, lpszSourceFile, _T("rb")) != 0)
	{
		goto exit;
	}

	if (fopen_s(&hDestFile, lpszDestinationFile, _T("wb")) != 0)
	{
		goto exit;
	}

	if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
	{
		DWORD ret = GetLastError();
		if (ret != NTE_BAD_KEYSET)
		{
			goto exit;
		}
		else
		{
			if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
			{
				goto exit;
			}
		}
	}

	if (lpszPassword == NULL)
	{
		fread(&dwKeyBlobLen, sizeof(DWORD), 1, hSrcFile);
		if (ferror(hSrcFile) || feof(hSrcFile))
		{
			goto exit;
		}

		if ((pbKeyBlob = (PBYTE)malloc(dwKeyBlobLen)) == NULL)
		{
			goto exit;
		}

		fread(pbKeyBlob, 1, dwKeyBlobLen, hSrcFile);

		if (ferror(hSrcFile) || feof(hSrcFile))
		{
			goto exit;
		}

		if (!CryptImportKey(hProv, pbKeyBlob, dwKeyBlobLen, 0, 0, &hKey))
		{
			goto exit;
		}
	}
	else
	{
		if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
		{
			goto exit;
		}

		if (!CryptHashData(hHash, (PBYTE)lpszPassword, _tcslen(lpszPassword), 0))
		{
			goto exit;
		}

		if (!CryptDeriveKey(hProv, CALG_RC2, hHash, 0, &hKey))
		{
			goto exit;
		}
	}

	if ((pbBuffer = (PBYTE)malloc(BUFFER_SIZE)) == NULL)
	{
		goto exit;
	}

	do
	{
		dwCount = fread(pbBuffer, 1, BLOCK_SIZE, hSrcFile);

		if (ferror(hSrcFile))
		{
			goto exit;
		}

		bEOF = feof(hSrcFile);

		if (!CryptDecrypt(hKey, 0, bEOF, 0, pbBuffer, &dwCount))
		{
			goto exit;
		}

		fwrite(pbBuffer, 1, dwCount, hDestFile);
		if (ferror(hDestFile))
		{
			goto exit;
		}

	} while (!bEOF);

	bReturn = TRUE;
exit:
	if (hSrcFile) fclose(hSrcFile);
	if (hDestFile) fclose(hDestFile);
	if (pbKeyBlob) free(pbKeyBlob);
	if (pbBuffer) free(pbBuffer);
	if (hKey) CryptDestroyKey(hKey);
	if (hHash) CryptDestroyHash(hHash);
	if (hProv) CryptReleaseContext(hProv, 0);

	return bReturn;
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值