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

📄 krcrypto.cpp

📁 windowxp 下各种系统提供的加密方法:rsa,des,sha,md5
💻 CPP
字号:
#include "StdAfx.h"

#include ".\krcrypto.h"

#include <atlbase.h>
#include <atlcrypt.h>
#include <atlenc.h>


CKrCrypto::CKrCrypto(void)
{
}

CKrCrypto::~CKrCrypto(void)
{
}

bool CKrCrypto::Base64Encode(std::string& strTxt)
{
	if ( strTxt.empty() )
		return false;

	int nSrcLen = strTxt.size();
	int nDesLen = Base64EncodeGetRequiredLength( nSrcLen );
	std::string strRlt( nDesLen, 0 );
	
	if ( !::Base64Encode((PBYTE)&strTxt[0], nSrcLen, &strRlt[0], &nDesLen) )
		return false;

	strRlt.resize(nDesLen);

	strTxt.swap(strRlt);

	return true;
}

bool CKrCrypto::Base64Decode(std::string& strTxt)
{
	if ( strTxt.empty() )
		return false;

	int nSrcLen = strTxt.size();
	int nDesLen = Base64DecodeGetRequiredLength( nSrcLen );
	std::string strRlt( nDesLen, 0 );
	
	if ( !::Base64Decode(&strTxt[0], nSrcLen, (PBYTE)&strRlt[0], &nDesLen) )
		return false;

	strRlt.resize(nDesLen);

	strTxt.swap(strRlt);

	return true;
}

#define IF_FAILED_RETURN_FALSE(x) if ( FAILED(x) ) return false;

bool CKrCrypto::GenRsaKeyPair(std::string& strPub, std::string& strPrv, const int& nKeySize)
{
	CCryptProv Prov;
	IF_FAILED_RETURN_FALSE( Prov.Initialize(PROV_RSA_FULL, NULL, MS_STRONG_PROV) );

	CCryptRandomKey KeyPair;
	IF_FAILED_RETURN_FALSE( KeyPair.Initialize( Prov, CALG_RSA_KEYX, CRYPT_EXPORTABLE | (nKeySize << 16) ) );

	DWORD dwLen=0;

	IF_FAILED_RETURN_FALSE( KeyPair.ExportPrivateKeyBlob(CCryptKey::EmptyKey, CRYPT_OAEP, NULL, &dwLen) );
	strPrv.resize(dwLen);
	IF_FAILED_RETURN_FALSE( KeyPair.ExportPrivateKeyBlob(CCryptKey::EmptyKey, CRYPT_OAEP, (BYTE*)&strPrv[0], &dwLen) );
	strPrv.resize(dwLen);

	IF_FAILED_RETURN_FALSE( KeyPair.ExportPublicKeyBlob(CCryptKey::EmptyKey, CRYPT_OAEP, NULL, &dwLen) );
	strPub.resize(dwLen);
	IF_FAILED_RETURN_FALSE( KeyPair.ExportPublicKeyBlob(CCryptKey::EmptyKey, CRYPT_OAEP, (BYTE*)&strPub[0], &dwLen) );
	strPub.resize(dwLen);

	return true;
}

bool CKrCrypto::RandomEnCrypt(std::string& strText, const std::string& strPubKey, std::string& strRandKey)
{
	if ( strText.empty() || strPubKey.empty() )
		return false;

	CCryptProv Prov;
	IF_FAILED_RETURN_FALSE( Prov.Initialize(PROV_RSA_FULL, NULL, MS_STRONG_PROV) );

	CCryptImportKey PublicKey;
	IF_FAILED_RETURN_FALSE( PublicKey.Initialize(Prov, (BYTE*)&strPubKey[0], strPubKey.size(), CCryptKey::EmptyKey, CRYPT_OAEP) );

	CCryptRandomKey SessionKey;
	IF_FAILED_RETURN_FALSE( SessionKey.Initialize(Prov, CALG_3DES) );

	DWORD dwLenBuf = strText.size();
	DWORD dwLenRlt = dwLenBuf;
	
	IF_FAILED_RETURN_FALSE( SessionKey.Encrypt(TRUE, NULL, &dwLenRlt, 0) );
	strText.resize( dwLenRlt > dwLenBuf ? dwLenRlt : dwLenBuf );
	IF_FAILED_RETURN_FALSE( SessionKey.Encrypt(TRUE, (BYTE*)&strText[0], &dwLenBuf, strText.size()) );
	strText.resize(dwLenBuf);

	DWORD dwLenKey;
	IF_FAILED_RETURN_FALSE( SessionKey.ExportSimpleBlob(PublicKey, CRYPT_OAEP, NULL, &dwLenKey) );
	strRandKey.resize(dwLenKey);
	IF_FAILED_RETURN_FALSE( SessionKey.ExportSimpleBlob(PublicKey, CRYPT_OAEP, (BYTE*)&strRandKey[0], &dwLenKey) );
	strRandKey.resize(dwLenKey);

	return true;
}

bool CKrCrypto::RandomDeCrypt(std::string& strText, const std::string& strPrvKey, const std::string& strRandKey)
{
	if ( strText.empty() || strPrvKey.empty() || strRandKey.empty() )
		return false;

	CCryptProv Prov;
	IF_FAILED_RETURN_FALSE( Prov.Initialize(PROV_RSA_FULL, NULL, MS_STRONG_PROV) );

	CCryptImportKey PrivateKey;
	IF_FAILED_RETURN_FALSE( PrivateKey.Initialize(Prov, (BYTE*)&strPrvKey[0], strPrvKey.size(), CCryptKey::EmptyKey, CRYPT_OAEP) );

	CCryptImportKey SessionKey;
	IF_FAILED_RETURN_FALSE( SessionKey.Initialize(Prov, (PBYTE)&strRandKey[0], strRandKey.size(), PrivateKey, CRYPT_OAEP) );

	DWORD dwLenData = strText.size();
	IF_FAILED_RETURN_FALSE( SessionKey.Decrypt(TRUE, (BYTE*)&strText[0], &dwLenData) );
	strText.resize(dwLenData);

	return true;
}

bool CKrCrypto::Md5Hash(const std::string& strText, std::string& strMd5)
{
	if ( strText.empty() )
		return false;

	CCryptProv Prov;
	IF_FAILED_RETURN_FALSE( Prov.Initialize(PROV_RSA_FULL, NULL, MS_STRONG_PROV) );

	CCryptMD5Hash hash;
	IF_FAILED_RETURN_FALSE( hash.Initialize(Prov) );

	IF_FAILED_RETURN_FALSE( hash.AddData((PBYTE)&strText[0], strText.size()) );

	DWORD dwSize = 0;
	IF_FAILED_RETURN_FALSE( hash.GetSize(&dwSize) );

	strMd5.resize(dwSize);

	IF_FAILED_RETURN_FALSE( hash.GetValue((PBYTE)&strMd5[0], &dwSize) );
	strMd5.resize(dwSize);

	return true;
}

bool CKrCrypto::Sha1Hash(const std::string& strText, std::string& strSha1)
{
	if ( strText.empty() )
		return false;

	CCryptProv Prov;
	IF_FAILED_RETURN_FALSE( Prov.Initialize(PROV_RSA_FULL, NULL, MS_STRONG_PROV) );

	CCryptSHA1Hash hash;
	IF_FAILED_RETURN_FALSE( hash.Initialize(Prov) );

	IF_FAILED_RETURN_FALSE( hash.AddData((PBYTE)&strText[0], strText.size()) );

	DWORD dwSize = 0;
	IF_FAILED_RETURN_FALSE( hash.GetSize(&dwSize) );

	strSha1.resize(dwSize);

	IF_FAILED_RETURN_FALSE( hash.GetValue((PBYTE)&strSha1[0], &dwSize) );
	strSha1.resize(dwSize);

	return true;
}

void CKrCrypto::Hex16Encode(std::string& strText)
{
	if ( strText.empty() )
		return;

	DWORD dwLen = strText.size();
	std::string strHex(dwLen * 2, 0);

	for( DWORD i = 0; i < dwLen; ++i )
	{
		char ch[3] = {0};
		sprintf(ch, "%02X", (BYTE)strText[i]);
		memcpy(&strHex[i*2], ch, 2);
	}

	strHex.swap(strText);
}

bool CKrCrypto::Hex16Decode(std::string& strText)
{
	if ( strText.empty() || strText.size() % 2 != 0 )
		return false;

	DWORD dwLen = strText.size() / 2;
	std::string strHex(dwLen, 0);

	for( DWORD i = 0; i < dwLen; ++i )
	{
		char ch[3] = {0};
		memcpy(ch, &strText[i*2], 2);

		int nData = 0;
		if ( 0 == sscanf(ch, "%X", &nData) )
			return false;

		strHex[i] = nData;
	}

	strHex.swap(strText);

	return true;
}

bool CKrCrypto::SimpleEnCrypt(std::string& strText, const std::string& strKey)
{
	if ( strText.empty() || strKey.empty() )
		return false;

	DWORD dwLenKey = strKey.size();
	DWORD dwLenData = strText.size();

	for ( DWORD i = 0; i < dwLenData; ++i )
		strText[i] ^= strKey[i % dwLenKey];

	return true;
}

bool CKrCrypto::RsaEnCrypt(std::string& strText, const std::string& strKey)
{
	if ( strText.empty() || strKey.empty() )
		return false;

	CCryptProv Prov;
	IF_FAILED_RETURN_FALSE( Prov.Initialize(PROV_RSA_FULL, NULL, MS_STRONG_PROV) );

	CCryptImportKey rsaKey;
	IF_FAILED_RETURN_FALSE( rsaKey.Initialize(Prov, (BYTE*)&strKey[0], strKey.size(), CCryptKey::EmptyKey, CRYPT_OAEP) );

	DWORD dwLenBuf = strText.size();
	DWORD dwLenRlt = dwLenBuf;

	IF_FAILED_RETURN_FALSE( rsaKey.Encrypt(TRUE, NULL, &dwLenRlt, 0) );
	strText.resize( dwLenRlt > dwLenBuf ? dwLenRlt : dwLenBuf );
	IF_FAILED_RETURN_FALSE( rsaKey.Encrypt(TRUE, (BYTE*)&strText[0], &dwLenBuf, strText.size()) );
	strText.resize(dwLenBuf);

	return true;
}

bool CKrCrypto::RsaDecrypt(std::string& strText, const std::string& strKey)
{
	if ( strText.empty() || strKey.empty() )
		return false;

	CCryptProv Prov;
	IF_FAILED_RETURN_FALSE( Prov.Initialize(PROV_RSA_FULL, NULL, MS_STRONG_PROV) );

	CCryptImportKey rsaKey;
	IF_FAILED_RETURN_FALSE( rsaKey.Initialize(Prov, (BYTE*)&strKey[0], strKey.size(), CCryptKey::EmptyKey, CRYPT_OAEP) );

	DWORD dwLenData = strText.size();
	IF_FAILED_RETURN_FALSE( rsaKey.Decrypt(TRUE, (BYTE*)&strText[0], &dwLenData) );
	strText.resize(dwLenData);

	return true;
}

⌨️ 快捷键说明

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