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

📄 testdialogdlg.cpp

📁 how to use MSCSP encrypt and decrypt
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
		MessageBox("Out of memory.");
		return NULL;
	}

	dwCount = strEncrypting.GetLength();
	for(int i = 0; i < (int)dwCount; i++)
	{
		pbBuffer[i] = strEncrypting[i];
	}
	pbBuffer[i] = '\0';

	//enctrpt data
/*	try{
	CryptEncrypt(
		hKey,			//session key;
		0,				//if hash and encrypt simulaly then input the hash object
		TRUE,	        //if last encrypted block is TRUE, else is FALSE.
		0,				//reserved parameter
		pbBuffer,		//the input the encrypting data and output the encrypted data
		&dwCount,		//the length of encrypting data and output the length of encrypted data
		dwBufferLen);	//pbBuffer size
	}
	catch(CException ex)
	{
		ex.ReportError();
	}*/
	if(!CryptEncrypt(
		hKey,			//session key;
		0,				//if hash and encrypt simulaly then input the hash object
		TRUE,	        //if last encrypted block is TRUE, else is FALSE.
		0,				//reserved parameter
		pbBuffer,		//the input the encrypting data and output the encrypted data
		&dwCount,		//the length of encrypting data and output the length of encrypted data
		dwBufferLen))	//pbBuffer size
	{ 
		DWORD DW = GetLastError(); 
		return NULL;
	}

//	m_strDecryptingData.Format("%s", pbBuffer);

	return pbBuffer;
//	if(hKey)      CryptDestroyKey(hKey);
//	m_strEncryptingData = _T("");
//	UpdateData(FALSE);
}

BYTE* CTestDialogDlg::DecryptData(HCRYPTKEY hKey, CString strDecryptingData)
{
	//Create the session key
//	char* pzPasswords = (LPTSTR)(LPCTSTR)m_strPassword;
//	HCRYPTKEY hKey = NULL;
//	hKey = CreateSessionKey(pzPasswords, ENCRYPT_ALGORITHM, CRYPT_CREATE_SALT);

	BYTE* pbBuffer;
	DWORD dwBufferLen = 0;
	DWORD dwCount;
	DWORD dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
	//--------------------------------------------------------------------
	// Determine the block size. If a block cipher is used, 
	// it must have room for an extra block. 	
	if(ENCRYPT_BLOCK_SIZE > 1) 
		dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; 
	else 
		dwBufferLen = dwBlockLen; 
	
	// Allocate memory. 
	pbBuffer = (BYTE *)malloc(dwBufferLen);
	if(pbBuffer == NULL)
	{
		MessageBox("Out of memory.");
		return NULL;
	}

	dwCount = strDecryptingData.GetLength();
	for(int i = 0; i < (int)dwCount; i++)
	{
		pbBuffer[i] = strDecryptingData[i];
	}
	pbBuffer[i] = '\0';

	try
	{
	CryptDecrypt(
		hKey,			//session key;
		0,				//if hash and encrypt simulaly then input the hash object
		TRUE,	        //if last encrypted block is TRUE, else is FALSE.
		0,				//reserved parameter
		pbBuffer,		//the input the encrypting data and output the encrypted data
		&dwCount);		//the length of encrypting data and output the length of encrypted data
	}
	catch(CException ex)
	{
		ex.ReportError();
	}


/*	//enctrpt data
	if(!CryptDecrypt(
		hKey,			//session key;
		0,				//if hash and encrypt simulaly then input the hash object
		TRUE,	        //if last encrypted block is TRUE, else is FALSE.
		0,				//reserved parameter
		pbBuffer,		//the input the encrypting data and output the encrypted data
		&dwCount))		//the length of encrypting data and output the length of encrypted data
	{
		MessageBox("Error during CryptDecrypt. \n");
		return NULL;
	} 
*/
//	m_strEncryptingData.Format("%s", pbBuffer);

	pbBuffer[dwCount] = '\0';
	return pbBuffer;
//	if(hKey)      CryptDestroyKey(hKey);
//	m_strDecryptingData = _T("");
//	UpdateData(FALSE);
}

void CTestDialogDlg::OnChangeEdit3() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	EnableScreen(TRUE, FALSE);
}

HCRYPTKEY CTestDialogDlg::GetKeyFromContainer(HCRYPTPROV hProv)
{
	HCRYPTKEY hKey = NULL;
	if(!CryptGetUserKey(   
		hProv,    
		AT_KEYEXCHANGE,    
		&hKey)) 
	{
		if(GetLastError() == NTE_NO_KEY) //Non-Exist signature key pair, create it.
		{
			if(!CryptGenKey(
				hProv,			//CSP句柄
				AT_KEYEXCHANGE,	//创建的密钥对类型为signature key pair
				0,				//key类型,这里用默认值
				&hKey)) 		//创建成功返回新创建的密钥对的句柄
			{
				MessageBox("Cannot Create a new Signature Key pair."); 
				return NULL;
			}
		}
		else
		{
			MessageBox("Error during CryptGetUserKey for signkey.");
			return NULL;
		}
	}

	return hKey;
}

void CTestDialogDlg::ExportPublicKey(HCRYPTKEY hKey)
{
	BYTE* pbKeyBlob;
	DWORD dwBlobLen;

	//Get the length of public Key
	if(!CryptExportKey(   
		hKey,    
		NULL,    
		PUBLICKEYBLOB,
		0,    
		NULL, 
		&dwBlobLen))
	{
		MessageBox("Error computing BLOB length.");
		return;
	}

	if((pbKeyBlob = (BYTE*)malloc(dwBlobLen)) == NULL) 
	{
		MessageBox("Out of memory.");
	}
	//Save public key data into paratmeter pbKeyBlob.
	if(!CryptExportKey(   
		hKey, 
		NULL,    
		PUBLICKEYBLOB,    
		0,    
		pbKeyBlob,    
		&dwBlobLen))
	{
		MessageBox("Error during CryptExportKey.");
		return;
	}

	m_dwKeyBlobLen = dwBlobLen;
	m_pbKeyBlob = (BYTE*)malloc(dwBlobLen + 1);
	for(int i = 0; i < (int)dwBlobLen; i++)
	{
		m_pbKeyBlob[i] = pbKeyBlob[i];
	}
	m_pbKeyBlob[i] = '\0';

//	strcpy(m_pbKeyBlob, pbKeyBlob);
}

HCRYPTHASH CTestDialogDlg::AddDataToHash(HCRYPTPROV hProv, CString strSignatureData)
{
	if(NULL == hProv) return NULL;

	HCRYPTHASH hHash = NULL;
	if(!CryptCreateHash(
		hProv, 
		CALG_MD5, 
		0, 
		0, 
		&hHash)) 
	{
		MessageBox("Error during CryptCreateHash.");
		return NULL;
	}

	DWORD dwBufferLen = strSignatureData.GetLength();
	BYTE* pbBuffer = (BYTE *)malloc(dwBufferLen + 1);
	for(int i = 0; i < (int)dwBufferLen; i++)
	{
		pbBuffer[i] = strSignatureData[i];
	}
	pbBuffer[i] = '\0';
	// adds data to a specified hash object.
	if(!CryptHashData(
		hHash, 
		pbBuffer,
		dwBufferLen, 
		0)) 
	{
		MessageBox("Error during CryptHashData.");
		return NULL;
	}

	return hHash;
}

BYTE* CTestDialogDlg::GetSignatureData(HCRYPTHASH hHash)
{
	if(NULL == hHash)  return NULL;

	BYTE* pbSignature;
	DWORD dwSigLen= 0;

	// 使用signature key pair的私钥对hash数据签名
	if(!CryptSignHash(
		hHash, 
		AT_SIGNATURE, 
		NULL, 
		0, 
		NULL, 
		&dwSigLen)) //得到数字签名大小
	{
		MessageBox("Error during CryptSignHash.");
		return NULL;
	}

	//--------------------------------------------------------------------
	// 为数字签名缓冲区分配内存
	if((pbSignature = (BYTE *)malloc(dwSigLen)) == NULL)
	{
		MessageBox("Out of memory.");
		return NULL;
	}

	//--------------------------------------------------------------------
	// 得到数字签名
	if(!CryptSignHash(
		hHash, 
		AT_SIGNATURE, 
		NULL, 
		0, 
		pbSignature, //这里将返回数字签名,同被签名的数据一起发送给接收方
		&dwSigLen)) 
	{
		MessageBox("Error during CryptSignHash.");
		return NULL;
	}

	return pbSignature;
}

void CTestDialogDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	HCRYPTKEY hKey = GetKeyFromContainer(m_hCryptProv);
//	ExportPublicKey(hKey);
//	m_strPublicKey.Format("%s", m_pbKeyBlob);

	BYTE* pbEncryptedData = EncryptData(hKey, m_strSignatingData);
	m_strSignatedData.Format("%s", pbEncryptedData);
	m_strSignatingData = _T("");
/*	HCRYPTHASH hHash = AddDataToHash(m_hCryptProv, m_strSignatingData);
	BYTE* pbSignatureData = GetSignatureData(hHash);
	m_strSignatedData.Format("%s", pbSignatureData);
*/
	if(hKey)   CryptDestroyKey(hKey);
//	if(hHash)  CryptDestroyHash(hHash);

	UpdateData(FALSE);	
}

void CTestDialogDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
// 	HCRYPTKEY hKey = ImportPublicKey(m_hCryptProv, m_pbKeyBlob, m_dwKeyBlobLen);
	HCRYPTKEY hKey = GetKeyFromContainer(m_hCryptProv);
	BYTE* pbDecryptedData = DecryptData(hKey, m_strSignatedData);
	m_strSignatingData.Format("%s", pbDecryptedData);
	m_strSignatedData = _T("");
/*	HCRYPTHASH hHash = AddDataToHash(m_hCryptProv, m_strSignatingData);
	BYTE* pbSignatureData = GetSignatureData(hHash);
	m_strSignatedData.Format("%s", pbSignatureData);
*/
	if(hKey)   CryptDestroyKey(hKey);
//	if(hHash)  CryptDestroyHash(hHash);

	UpdateData(FALSE);	
}

HCRYPTKEY CTestDialogDlg::ImportPublicKey(HCRYPTPROV hProv, BYTE* pbKeyBlob, DWORD dwBlobLen)
{
	if(NULL == hProv)  return NULL;
	
	HCRYPTKEY hPubKey = NULL;
	if(!CryptImportKey(
		hProv,
		pbKeyBlob,
		dwBlobLen,
		0,
		0,
		&hPubKey))
	{
		MessageBox("Public key import failed.");
		return NULL;
	}

	return hPubKey;
}

BYTE* CTestDialogDlg::GetVarifySignatureData(HCRYPTHASH hHash, HCRYPTKEY hPubKey, CString strSignatedData)
{
	if(NULL == hHash)  return NULL;

	DWORD dwSigLen = strSignatedData.GetLength();
	BYTE* pbSignature = (BYTE*)malloc(dwSigLen + 1);
	for(int i = 0; i < (int)dwSigLen; i++)
	{
		pbSignature[i] = strSignatedData[i];
	}
	pbSignature[i] = '\0';
	// 验证数字签名
	if(CryptVerifySignature(
		hHash, 
		pbSignature,	//数字签名数据
		dwSigLen, 
		hPubKey,		//签名者的公钥
		NULL, 
		0)) 
	{
		MessageBox("Signature not validated!\n");
		return NULL;
	}

	return pbSignature;
}

⌨️ 快捷键说明

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