Click here to Skip to main content
15,888,337 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionReg : inheritance Pin
pooja_friends5-Dec-08 1:19
pooja_friends5-Dec-08 1:19 
AnswerRe: Reg : inheritance Pin
Code-o-mat5-Dec-08 1:29
Code-o-mat5-Dec-08 1:29 
GeneralRe: Reg : inheritance Pin
pooja_friends5-Dec-08 2:00
pooja_friends5-Dec-08 2:00 
GeneralRe: Reg : inheritance Pin
Code-o-mat5-Dec-08 2:09
Code-o-mat5-Dec-08 2:09 
AnswerRe: Reg : inheritance Pin
CPallini5-Dec-08 2:01
mveCPallini5-Dec-08 2:01 
QuestionControlling Display Device Pin
Muneeb Abdul Shakoor5-Dec-08 1:01
Muneeb Abdul Shakoor5-Dec-08 1:01 
AnswerRe: Controlling Display Device Pin
Mark Salsbery5-Dec-08 4:53
Mark Salsbery5-Dec-08 4:53 
QuestionPlease help me with my decryption it almost works Pin
monsieur_jj4-Dec-08 23:23
monsieur_jj4-Dec-08 23:23 
This is my code:
It gives me true when I perform the function crypt decrypt its just that I am expecting this output : aHR0cDovL2xvY2FsaG9zdDo4MDgwL015V2ViL21hbmFnZXBvaW50L21hbmFnZXBvaW50MzQxLnppcA==
Then when I decode it Ill should get a link but instead cryptdecrypt gives me trash which I cant convert to the right link
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 *dwBufSize)
{
	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;
			}
			*dwBufSize = dwResult;
			return lpszBase64;
		}
	}
	return NULL;
}


BYTE* Base64Decode2(LPCTSTR lpData, DWORD dwSize, DWORD *dwBufSize)
{
	DWORD dwResult = 0;
	if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
	{
		BYTE * decodedStr;
		decodedStr = new BYTE;
		LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
		memset(decodedStr,0,dwResult);
		if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,decodedStr, &dwResult,NULL,NULL))
		{
			*dwBufSize = dwResult;
			return decodedStr;
		}
	}
	return NULL;
}
LPTSTR Base64Encode2(BYTE* lpData, DWORD dwSize, BOOL bStripCRLF, DWORD *dwbufSize)
{
	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;
			}
			*dwbufSize = dwResult;
			return lpszBase64;
		}
	}
	return NULL;
}
DWORD TripleDESdecrypt(TCHAR *cyphertext,DWORD ctlen,TCHAR *passwd,DWORD pwlen,TCHAR *plaintext,DWORD *ptlen)
{
	HCRYPTPROV hProv;
	HCRYPTHASH hHash;
	HCRYPTKEY hKey;
	DWORD dwSizeNeeded =0;

	BOOL result = CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,0);
	result = CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
	result = CryptHashData(hHash,(BYTE *)passwd,pwlen,0);
	result = CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
	BYTE PadMode = CRYPT_MODE_ECB;
	result = CryptSetKeyParam(hKey, PKCS5_PADDING, &PadMode, 0);
	if(*ptlen >= ctlen)
	{
		memcpy(plaintext,cyphertext,*ptlen);
		result = 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()
{
	
	BYTE* decodedStr;
	HCRYPTPROV hCryptProv;
	HCRYPTHASH hHash = 0;
	HCRYPTKEY hKey = 0;

	TCHAR szKey[] = _T("h3bmull3r");
	TCHAR szUnencrypted[] = _T("");
	TCHAR * pszEncrypted = NULL;
	TCHAR * pszBase64 = NULL;
	TCHAR * pszBase64Decoded = NULL;
	TCHAR * psz3DESDecoded = NULL;
	DWORD dwSizeNeeded;
	DWORD BufSize;
	DWORD dwUnEncryptedSize;
	DWORD dwEncryptedSize;
	DWORD KeySize;

	////convert Key
	TCHAR * NewKey  = Base64Encode((LPCBYTE)&szKey,_tcslen(szKey) * sizeof(TCHAR),FALSE, &KeySize);
	_tcscpy(szKey, NewKey);

	pszBase64 = _T("bZVLVixsjgTxsWbwmAGitRR0T86AM9Kam4BYRR+Dukc9p9vhwUBAzCSlZ463ZInufJRE+7ridImlz/aJUJbVhg==");

	BYTE* TEST = Base64Decode2(pszBase64,_tcslen(pszBase64) * sizeof(TCHAR), &dwEncryptedSize);
	TCHAR * pszEncrypted2 = reinterpret_cast<TCHAR *>(TEST);
	pszEncrypted = pszEncrypted2;
	//Decrypt the string with 3DES algorithm using key
	dwUnEncryptedSize = NULL;
	dwSizeNeeded = TripleDESdecrypt(pszEncrypted,dwEncryptedSize * sizeof(TCHAR),szKey,_tcslen(szKey),szUnencrypted,&dwUnEncryptedSize);
	psz3DESDecoded = new TCHAR[dwSizeNeeded];
	TripleDESdecrypt(pszEncrypted,dwEncryptedSize * sizeof(TCHAR),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;
	//delete [] pszEncrypted2;

	return 0;
}


My code is a c++ version of this code : http://www.codeproject.com/KB/web-security/Cryptography_MD5_TriDES.aspx?display=Print[^]

Please help me I really need this I did all what I can.

Thanks,
Jayjay
AnswerRe: Please help me with my decryption it almost works Pin
CPallini5-Dec-08 11:08
mveCPallini5-Dec-08 11:08 
GeneralRe: Please help me with my decryption it almost works Pin
monsieur_jj5-Dec-08 14:47
monsieur_jj5-Dec-08 14:47 
GeneralRe: Please help me with my decryption it almost works Pin
Luc Pattyn5-Dec-08 11:47
sitebuilderLuc Pattyn5-Dec-08 11:47 
AnswerRe: Please help me with my decryption it almost works Pin
Randor 5-Dec-08 21:10
professional Randor 5-Dec-08 21:10 
GeneralRe: Please help me with my decryption it almost works Pin
monsieur_jj7-Dec-08 19:08
monsieur_jj7-Dec-08 19:08 
QuestionStatic problem Pin
NewVC++4-Dec-08 22:21
NewVC++4-Dec-08 22:21 
AnswerRe: Static problem Pin
Iain Clarke, Warrior Programmer4-Dec-08 22:27
Iain Clarke, Warrior Programmer4-Dec-08 22:27 
AnswerRe: Static problem Pin
Jijo.Raj5-Dec-08 3:17
Jijo.Raj5-Dec-08 3:17 
AnswerRe: Static problem Pin
Hamid_RT5-Dec-08 6:33
Hamid_RT5-Dec-08 6:33 
Questionhow to find if the file is transferred [modified] Pin
Dhiraj kumar Saini4-Dec-08 22:05
Dhiraj kumar Saini4-Dec-08 22:05 
QuestionRe: how to find if the file is transferred Pin
David Crow5-Dec-08 3:01
David Crow5-Dec-08 3:01 
QuestionWalk through html controls Pin
Varghese Paul M4-Dec-08 21:28
Varghese Paul M4-Dec-08 21:28 
AnswerRe: Walk through html controls Pin
Rajesh R Subramanian4-Dec-08 21:46
professionalRajesh R Subramanian4-Dec-08 21:46 
Question[Message Deleted] Pin
abc_star_14-Dec-08 20:34
abc_star_14-Dec-08 20:34 
AnswerRe: Invoking webservice from C++ Application Pin
Malli_S4-Dec-08 20:44
Malli_S4-Dec-08 20:44 
General[Message Deleted] Pin
abc_star_14-Dec-08 20:56
abc_star_14-Dec-08 20:56 
GeneralRe: Invoking webservice from C++ Application Pin
Malli_S4-Dec-08 21:11
Malli_S4-Dec-08 21:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.