⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binencrypt.cpp

📁 cryptapi算法 在这个信息爆炸的时代
💻 CPP
字号:
// BinEncrypt.cpp: implementation of the CBinEncrypt class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BinEncrypt.h"

#define _WIN32_WINNT 0x0400

#include <Wincrypt.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBinEncrypt::CBinEncrypt()
{
}

CBinEncrypt::~CBinEncrypt()
{
}

bool CBinEncrypt::Crypt( LPCTSTR lpszSrc, LPCTSTR lpszKey, LPBYTE lpDest, DWORD dwDestSize )
{
	DWORD dwLength = _tcslen(lpszSrc) * sizeof(TCHAR);
	if ( dwDestSize < dwLength )
		return false;
	try
	{
		memcpy( lpDest, lpszSrc, dwLength );
		return Crypt( lpDest, dwLength, lpszKey );
	}
	catch(...)
	{
	}

	return false;
}

bool CBinEncrypt::Crypt( LPBYTE lpData, DWORD dwDataSize, LPCTSTR lpszKey )
{
	bool bResult = false;

	HCRYPTPROV hProv = NULL;
	HCRYPTKEY hKey = NULL;
	HCRYPTKEY hXchgKey = NULL;
	HCRYPTHASH hHash = NULL;
	DWORD dwLength;

	// Get handle to user default provider.
	if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
	{
		// Create hash object.
		if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
		{
			// Hash password string.
			dwLength = sizeof(TCHAR) * _tcslen(lpszKey);
			if (CryptHashData(hHash, (BYTE*)lpszKey, dwLength, 0))
			{
				// Create block cipher session key based on hash of the password.
				if (CryptDeriveKey(hProv, CALG_RC4, hHash, CRYPT_EXPORTABLE, &hKey))
				{
					// Encrypt data
					dwLength = dwDataSize;
					if (CryptEncrypt(hKey, 0, TRUE, 0, lpData, &dwLength, dwLength))
					{
						bResult = true;
					}
					CryptDestroyKey(hKey);  // Release provider handle.	
				}
			}
			CryptDestroyHash(hHash);
			// Destroy session key.
		}

		CryptReleaseContext(hProv, 0);
	}

	return bResult;
}

bool CBinEncrypt::Decrypt( LPBYTE lpSrc, DWORD dwSrcSize, LPCTSTR lpszKey, LPTSTR lpszDest )
{
	LPBYTE lpBuf = new BYTE[dwSrcSize];
	if ( lpBuf == NULL )
		return false;
	try
	{
		memcpy( lpBuf, lpSrc, dwSrcSize );
		if ( !Decrypt( lpBuf, dwSrcSize, lpszKey ) )
			throw 1;

		memcpy( lpszDest, lpBuf, dwSrcSize );
		delete[] lpBuf;

		return true;
	}
	catch(...)
	{
		delete[] lpBuf;
	}
	return false;
}

bool CBinEncrypt::Decrypt( LPBYTE lpData, DWORD dwDataSize, LPCTSTR lpszKey )
{
	bool bResult = false;

	HCRYPTPROV hProv = NULL;
	HCRYPTKEY hKey = NULL;
	HCRYPTKEY hXchgKey = NULL;
	HCRYPTHASH hHash = NULL;
	DWORD dwLength;

	// Get handle to user default provider.
	if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
	{
		// Create hash object.
		if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
		{
			// Hash password string.
			dwLength = sizeof(TCHAR) * _tcslen(lpszKey);
			if (CryptHashData(hHash, (BYTE*)lpszKey, dwLength, 0))
			{
				// Create block cipher session key based on hash of the password.
				if (CryptDeriveKey(hProv, CALG_RC4, hHash, CRYPT_EXPORTABLE, &hKey))
				{
					// the password is less than 32 characters
					dwLength = dwDataSize;

					if (CryptDecrypt(hKey, 0, TRUE, 0, (BYTE*)lpData, &dwLength))
						bResult = true;

					CryptDestroyKey(hKey);  // Release provider handle.
				}
			}
			CryptDestroyHash(hHash); // Destroy session key.
		}
		CryptReleaseContext(hProv, 0);
	}

	return bResult;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -