我是靠谱客的博主 淡淡战斗机,最近开发中收集的这篇文章主要介绍Windows自带的加密算法Crypto实现MD5, AES256以及RSA算法1. MD52. AES3. RSA,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

密码学我了解的很少,但是在逆向中这却是非常重要的基础。这个Crypto函数库是windows自带的,我实现了散列算法MD5[128位], 对称加密算法AES[256位]以及RSA算法。散列函数实现改几个参数就能成SHA算法了。

1. MD5

// 单向散列函数
BOOL OneWayHash(PBYTE pData, DWORD dwSize, ALG_ID cType)
{
	HCRYPTPROV hCryptProv = NULL;
	HCRYPTHASH hCryptHash = NULL;
	PBYTE pHashData = NULL;
	BOOL fOk = FALSE;
	DWORD dwHashLen = 0, dwDataLen = sizeof(dwHashLen);

	do
	{
		fOk = CryptAcquireContextA(&hCryptProv,
			NULL,
			NULL,
			PROV_RSA_AES,
			CRYPT_VERIFYCONTEXT
		);
		if (!fOk)
		{
			break;
		}
		// 创建一个空的HASH对象
		fOk = CryptCreateHash(hCryptProv, cType, NULL, 0, &hCryptHash);
		if (!fOk)
		{
			break;
		}
		// 生成对应的哈希值
		if (!CryptHashData(hCryptHash, pData, dwSize, 0))
		{
			break;
		}
		if (!CryptGetHashParam(hCryptHash, HP_HASHSIZE, (PBYTE)&dwHashLen, &dwDataLen, 0))
		{
			break;
		}
		pHashData = new BYTE[dwHashLen];
		if (NULL == pHashData)
		{
			break;
		}
		if (!CryptGetHashParam(hCryptHash, HP_HASHVAL, pHashData, &dwHashLen, 0))
		{
			break;
		}
		fOk = TRUE;
	} while (FALSE);

	if (fOk)
	{
		for (size_t nIdx = 0; nIdx < dwHashLen; ++nIdx)
		{
			printf("%02X", pHashData[nIdx]);
		}
		putchar('n');
	}
	if (NULL != pHashData)
	{
		delete[] pHashData;
		pHashData = NULL;
	}
	if (NULL != hCryptProv)
	{
		CryptReleaseContext(hCryptProv, 0);
		hCryptProv = NULL;
	}
	if (NULL != hCryptHash)
	{
		CryptDestroyHash(hCryptHash);
		hCryptHash = NULL;
	}
	
	return(fOk);
}

2. AES

BOOL FileWriteBack(const char *pcszFilePathName, PBYTE pbData, size_t nBufSize)
{
	BOOL fOk = FALSE;
	FILE *fp = NULL;

	fp = fopen(pcszFilePathName, "wb");
	if (NULL == fp)
	{
		return(fOk);
	}
	fwrite(pbData, nBufSize, 1, fp);
	if (NULL != fp)
	{
		fclose(fp);
		fp = NULL;
	}
	fOk = TRUE;

	return(fOk);
}


BOOL ReadFromFile(LPCSTR lpcszFilePathName, PBYTE *ppData, DWORD &dwFileSize, size_t &nFileBufLen)
{
	BOOL fOk = FALSE;
	FILE *fp = NULL;
	LONG lSize = 0;
	PBYTE pbData = NULL;

	fp = fopen(lpcszFilePathName, "rb");
	if (NULL == fp)
	{
		return(FALSE);
	}
	fseek(fp, 0, SEEK_END);
	lSize = ftell(fp);
	pbData = new BYTE[lSize * 16];
	RtlZeroMemory(pbData, lSize * 16);
	dwFileSize = lSize;
	nFileBufLen = lSize * 16;
	fseek(fp, 0, SEEK_SET);
	fread(pbData, lSize, 1, fp);
	if (NULL != fp)
	{
		fclose(fp);
		fp = NULL;
	}
	*ppData = pbData;

	return(TRUE);
}


// 256位AES算法加密与解密
BOOL AESEncrypt(const PBYTE pcszPassword, size_t nPassSize, LPCSTR lpcszOrigFilePathName, LPCSTR lpcszEncFilePathName)
{
	HCRYPTPROV hCryptProv = NULL;
	HCRYPTHASH hCryptHash = NULL;
	HCRYPTKEY hCryptKey = NULL;
	BOOL fOk = FALSE;
	PBYTE pbData = NULL;
	DWORD dwSize = 0;
	size_t nBufLen = 0;

	do
	{
		if (!ReadFromFile(lpcszOrigFilePathName, &pbData, dwSize, nBufLen))
		{
			break;
		}
		if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
		{
			break;
		}
		if (!CryptCreateHash(hCryptProv, CALG_MD5, NULL, 0, &hCryptHash))
		{
			break;
		}
		if (!CryptHashData(hCryptHash, pcszPassword, nPassSize, 0))
		{
			break;
		}
		if (!CryptDeriveKey(hCryptProv, CALG_AES_256, hCryptHash, CRYPT_EXPORTABLE, &hCryptKey))
		{
			break;
		}
		if (!CryptEncrypt(hCryptKey, NULL, TRUE, 0, pbData, &dwSize, nBufLen))
		{
			break;
		}
		fOk = FileWriteBack(lpcszEncFilePathName, pbData, dwSize);
	} while (FALSE);
	if (NULL != hCryptProv)
	{
		CryptReleaseContext(hCryptProv, 0);
		hCryptProv = NULL;
	}
	if (NULL != hCryptHash)
	{
		CryptDestroyHash(hCryptHash);
		hCryptHash = NULL;
	}
	if (NULL != hCryptKey)
	{
		CryptDestroyKey(hCryptKey);
		hCryptKey = NULL;
	}
	if (NULL != pbData)
	{
		delete[] pbData;
		pbData = NULL;
	}
	
	return(fOk);
}

BOOL AESDecrypt(const PBYTE pcszPassword, 
	size_t nPassSize,
	LPCSTR lpcszOrigFilePathName, 
	LPCSTR lpcszEncFilePathName
	)
{
	BOOL fOk = FALSE;
	PBYTE pbData = NULL;
	DWORD dwFileLen = 0;
	size_t nBufSize = 0;
	HCRYPTPROV hCryptProv = NULL;
	HCRYPTHASH hCryptHash = NULL;
	HCRYPTKEY hCryptKey = NULL;

	do
	{
		if (!ReadFromFile(lpcszOrigFilePathName, &pbData, dwFileLen, nBufSize))
		{
			break;
		}
		if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
		{
			break;
		}
		if (!CryptCreateHash(hCryptProv, CALG_MD5, NULL, 0, &hCryptHash))
		{
			break;
		}
		if (!CryptHashData(hCryptHash, pcszPassword, nPassSize, 0))
		{
			break;
		}
		if (!CryptDeriveKey(hCryptProv, CALG_AES_256, hCryptHash, 0, &hCryptKey))
		{
			break;
		}
		if (!CryptDecrypt(hCryptKey, NULL, TRUE, 0, pbData, &dwFileLen))
		{
			break;
		}
		if (!FileWriteBack(lpcszEncFilePathName, pbData, dwFileLen))
		{
			break;
		}
		fOk = TRUE;
	} while (FALSE);
	if (NULL != pbData)
	{
		delete[] pbData;
		pbData = NULL;
	}
	if (NULL != hCryptProv)
	{
		CryptReleaseContext(hCryptProv, 0);
		hCryptProv = NULL;
	}
	if (NULL != hCryptHash)
	{
		CryptDestroyHash(hCryptHash);
		hCryptHash = NULL;
	}
	if (NULL != hCryptKey)
	{
		CryptDestroyKey(hCryptKey);
		hCryptKey = NULL;
	}

	return(fOk);
}

3. RSA

BOOL FileWriteBack(const char *pcszFilePathName, PBYTE pbData, size_t nBufSize)
{
	BOOL fOk = FALSE;
	FILE *fp = NULL;

	fp = fopen(pcszFilePathName, "wb");
	if (NULL == fp)
	{
		return(fOk);
	}
	fwrite(pbData, nBufSize, 1, fp);
	if (NULL != fp)
	{
		fclose(fp);
		fp = NULL;
	}
	fOk = TRUE;

	return(fOk);
}


BOOL ReadFromFile(LPCSTR lpcszFilePathName, PBYTE *ppData, DWORD &dwFileSize, size_t &nFileBufLen)
{
	BOOL fOk = FALSE;
	FILE *fp = NULL;
	LONG lSize = 0;
	PBYTE pbData = NULL;

	fp = fopen(lpcszFilePathName, "rb");
	if (NULL == fp)
	{
		return(FALSE);
	}
	fseek(fp, 0, SEEK_END);
	lSize = ftell(fp);
	pbData = new BYTE[lSize * 16];
	RtlZeroMemory(pbData, lSize * 16);
	dwFileSize = lSize;
	nFileBufLen = lSize * 16;
	fseek(fp, 0, SEEK_SET);
	fread(pbData, lSize, 1, fp);
	if (NULL != fp)
	{
		fclose(fp);
		fp = NULL;
	}
	*ppData = pbData;

	return(TRUE);
}

BOOL GenericKey(PBYTE *ppPublicKey, DWORD *pdwPublicKeyLength, PBYTE *ppPrivateKey, PDWORD pdwPrivateKeyLength)
{
	HCRYPTPROV hCryptProv = NULL;
	HCRYPTKEY hCryptKey = NULL;
	DWORD dwPrivateKeyLength = 0;
	DWORD dwPublicKeyLength = 0;
	PBYTE pPublicKey = NULL;
	PBYTE pPrivateKey = NULL;
	BOOL fOk = FALSE;

	do
	{
		if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
		{
			break;
		}
		// 生成公私钥对
		if (!CryptGenKey(hCryptProv, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hCryptKey))
		{
			break;
		}
		// 获取公钥的长度
		if (!CryptExportKey(hCryptKey, NULL, PUBLICKEYBLOB, 0, NULL, &dwPublicKeyLength))
		{
			break;
		}
		pPublicKey = new BYTE[dwPublicKeyLength];
		RtlZeroMemory(pPublicKey, dwPublicKeyLength);
		// 获取公钥
		if (!CryptExportKey(hCryptKey, NULL, PUBLICKEYBLOB, 0, pPublicKey, &dwPublicKeyLength))
		{
			break;
		}
		
		// 获取私钥长度
		if (!CryptExportKey(hCryptKey, NULL, PRIVATEKEYBLOB, 0, NULL, &dwPrivateKeyLength))
		{
			break;
		}
		pPrivateKey = new BYTE[dwPrivateKeyLength];
		RtlZeroMemory(pPrivateKey, dwPrivateKeyLength);
		// 生成私钥
		if (!CryptExportKey(hCryptKey, NULL, PRIVATEKEYBLOB, 0, pPrivateKey, &dwPrivateKeyLength))
		{
			break;
		}
		fOk = TRUE;
		*ppPublicKey = pPublicKey;
		*pdwPublicKeyLength = dwPublicKeyLength;
		*ppPrivateKey = pPrivateKey;
		*pdwPrivateKeyLength = dwPrivateKeyLength;
	} while (FALSE);

	if (hCryptKey)
	{
		CryptDestroyKey(hCryptKey);
	}
	if (hCryptProv)
	{
		CryptReleaseContext(hCryptProv, 0);
	}

	return(fOk);
}

BOOL RSAEncrypt(PBYTE pPublicKey, DWORD dwPublicKeyLength, PBYTE pData, DWORD &dwDataLength, DWORD dwBufferLength)
{
	BOOL fOk = FALSE;
	HCRYPTPROV hCryptProv = NULL;
	HCRYPTKEY hCryptKey = NULL;

	do
	{
		fOk = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0);
		if (!fOk)
		{
			break;
		}
		fOk = CryptImportKey(hCryptProv, pPublicKey, dwPublicKeyLength, NULL, 0, &hCryptKey);
		if (!fOk)
		{
			break;
		}
		fOk = CryptEncrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength, dwBufferLength);
	} while (FALSE);

	return(fOk);
}

BOOL RSADecrypt(PBYTE pPrivateKey, DWORD dwPrivateLength, PBYTE pData, DWORD &dwDataLength)
{
	BOOL fOk = FALSE;
	HCRYPTPROV hCryptProv = NULL;
	HCRYPTKEY hCryptKey = NULL;

	do
	{
		if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
		{
			break;
		}
		if (!CryptImportKey(hCryptProv, pPrivateKey, dwPrivateLength, NULL, 0, &hCryptKey))
		{
			break;
		}
		if (!CryptDecrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength))
		{
			break;
		}
		fOk = TRUE;
	} while (FALSE);
	if (hCryptKey)
	{
		CryptDestroyKey(hCryptKey);
	}
	if (hCryptProv)
	{
		CryptReleaseContext(hCryptProv, 0);
	}

	return(fOk);
}

(完)

最后

以上就是淡淡战斗机为你收集整理的Windows自带的加密算法Crypto实现MD5, AES256以及RSA算法1. MD52. AES3. RSA的全部内容,希望文章能够帮你解决Windows自带的加密算法Crypto实现MD5, AES256以及RSA算法1. MD52. AES3. RSA所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部