3des加密解密

#include "stdafx.h"
#include <windows.h>
#include <Wincrypt.h>
#include <assert.h>
 
#pragma comment(lib, "crypt32.lib")
 
LPTSTR Base64Decode(LPCTSTR lpData, DWORD dwSize)
{
	DWORD dwResult = 0;
	if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
	{
		LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
		memset(lpszBase64Decoded,0,dwResult);
		if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,(BYTE *)lpszBase64Decoded, &dwResult,NULL,NULL))
		{
			*(LPWORD)(lpszBase64Decoded + (dwResult / sizeof(TCHAR))) = 0;
 
			return lpszBase64Decoded;
		}
	}
	return NULL;
}
 
LPTSTR Base64Encode(LPCBYTE lpData, DWORD dwSize, BOOL bStripCRLF)
{
	DWORD dwResult = 0;
	if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult))
	{
		LPTSTR lpszBase64 = new TCHAR[dwResult];
		if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, lpszBase64, &dwResult))
		{
			TCHAR pByteLF = *(LPWORD)(lpszBase64 + dwResult -1);
			TCHAR pByteCR = *(LPWORD)(lpszBase64 + dwResult -2);
			if(pByteCR == 0x0D && pByteLF == 0x0A)
			{
				*(LPWORD)(lpszBase64 + dwResult -2) = 0;
			}
			return lpszBase64;
		}
	}
	return NULL;
}
 
DWORD TripleDESencrypt(TCHAR *plaintext,DWORD ptlen,TCHAR *passwd,DWORD pwlen,TCHAR *cyphertext,DWORD *ctlen)
{
	HCRYPTPROV hProv;
	HCRYPTHASH hHash;
	HCRYPTKEY hKey;
	DWORD dwSizeNeeded =0;
	DWORD sz =0;
 
	CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,0);
	CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
	CryptHashData(hHash,(BYTE*)passwd,pwlen,0);
	CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
 
	if(NULL != cyphertext && 0 != ptlen)
	{
		memcpy(cyphertext,plaintext,ptlen);
		sz=*ctlen;
		*ctlen=ptlen;
	}
 
	if(FALSE == CryptEncrypt(hKey,NULL,1,0,(BYTE *)cyphertext,ctlen,sz))
	{
		DWORD dwError = GetLastError();
		if(ERROR_MORE_DATA == dwError)
		{
			dwSizeNeeded = *ctlen * sizeof(TCHAR);
		}
	}
 
	CryptDestroyKey(hKey);
	CryptDestroyHash(hHash);
	CryptReleaseContext(hProv,0);
	return dwSizeNeeded;
}
 
DWORD TripleDESdecrypt(TCHAR *cyphertext,DWORD ctlen,TCHAR *passwd,DWORD pwlen,TCHAR *plaintext,DWORD *ptlen)
{
	HCRYPTPROV hProv;
	HCRYPTHASH hHash;
	HCRYPTKEY hKey;
	DWORD dwSizeNeeded =0;
 
	CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,0);
	CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
	CryptHashData(hHash,(BYTE *)passwd,pwlen,0);
	CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
	if(*ptlen >= ctlen)
	{
		memcpy(plaintext,cyphertext,*ptlen);
		CryptDecrypt(hKey,NULL,1,0,(BYTE *)plaintext,&ctlen);
		*ptlen=ctlen;
	}
	else
	{
		dwSizeNeeded = ctlen * sizeof(TCHAR);
	}
	CryptDestroyKey(hKey);
	CryptDestroyHash(hHash);
	CryptReleaseContext(hProv,0);
	return dwSizeNeeded;
}
 
int main()
{
	HCRYPTPROV hCryptProv;
	HCRYPTHASH hHash = 0;
	HCRYPTKEY hKey = 0;
 
	TCHAR szKey[] = _T("key");
	TCHAR szUnencrypted[MAX_PATH * sizeof(TCHAR)] = _T("test");
	TCHAR * pszEncrypted = NULL;
	TCHAR * pszBase64 = NULL;
	TCHAR * pszBase64Decoded = NULL;
	TCHAR * psz3DESDecoded = NULL;
	
	//Base64 encode the string
	pszBase64 = Base64Encode((LPCBYTE)&szUnencrypted,_tcslen(szUnencrypted) * sizeof(TCHAR),FALSE);
 
	//Encrypt the string with 3DES algorithm using key
	DWORD dwEncryptedSize = _tcslen(pszBase64);
	DWORD dwSizeNeeded = TripleDESencrypt(pszBase64,MAX_PATH * sizeof(TCHAR),szKey,_tcslen(szKey),pszBase64,&dwEncryptedSize);
	pszEncrypted = new TCHAR[dwSizeNeeded];
	TripleDESencrypt(pszBase64,MAX_PATH * sizeof(TCHAR),szKey,_tcslen(szKey),pszEncrypted,&dwEncryptedSize);
 
	//Decrypt the string with 3DES algorithm using key
	DWORD dwUnEncryptedSize = NULL;
	dwSizeNeeded = TripleDESdecrypt(pszEncrypted,dwSizeNeeded,szKey,_tcslen(szKey),szUnencrypted,&dwUnEncryptedSize);
	psz3DESDecoded = new TCHAR[dwSizeNeeded];
	TripleDESdecrypt(pszEncrypted,dwSizeNeeded,szKey,_tcslen(szKey),psz3DESDecoded,&dwSizeNeeded);
 
	//Decode the base64 encoded message back to ascii
	pszBase64Decoded = Base64Decode(psz3DESDecoded,_tcslen(psz3DESDecoded) * sizeof(TCHAR));
 
	//Do they match?
	MessageBox(NULL,0 == _tcscmp(pszBase64Decoded,szUnencrypted) ? _T("Strings Match"):_T("Strings do NOT Match"),_T(""),MB_OK);
 
	delete [] pszBase64;
	delete [] pszBase64Decoded;
	delete [] pszEncrypted;
 
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值