Click here to Skip to main content
15,917,005 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Setting grey color Pin
Alan Balkany24-Nov-08 3:59
Alan Balkany24-Nov-08 3:59 
QuestionHow do I add tabs like 3ds max [modified] Pin
akira3223-Nov-08 21:31
akira3223-Nov-08 21:31 
AnswerRe: How do I add tabs like 3ds max Pin
Cedric Moonen23-Nov-08 22:07
Cedric Moonen23-Nov-08 22:07 
AnswerRe: How do I add tabs like 3ds max Pin
Ahmed Charfeddine23-Nov-08 22:51
Ahmed Charfeddine23-Nov-08 22:51 
GeneralRe: How do I add tabs like 3ds max Pin
akira3211-Jan-09 15:29
akira3211-Jan-09 15:29 
GeneralRe: How do I add tabs like 3ds max Pin
Ahmed Charfeddine11-Jan-09 21:59
Ahmed Charfeddine11-Jan-09 21:59 
Questiondecrypting question Pin
monsieur_jj23-Nov-08 21:17
monsieur_jj23-Nov-08 21:17 
AnswerRe: decrypting question Pin
Randor 24-Nov-08 8:27
professional Randor 24-Nov-08 8:27 
Hello again JayJay,

I have written a sample for you. It doesn't get any better than this.

#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;
}


Smile | :)

I have tested it in both Unicode and ANSI and both seem to function as desired. You may want to clean my code up a bit.

Hope it helps,
-David Delaune
GeneralRe: decrypting question Pin
monsieur_jj24-Nov-08 18:23
monsieur_jj24-Nov-08 18:23 
QuestionDifference between OnPaint and OnDraw Pin
kDevloper23-Nov-08 21:03
kDevloper23-Nov-08 21:03 
AnswerRe: Difference between OnPaint and OnDraw Pin
Ahmed Charfeddine23-Nov-08 22:58
Ahmed Charfeddine23-Nov-08 22:58 
Questionclick event for custom tree control Pin
AnithaSubramani23-Nov-08 20:59
AnithaSubramani23-Nov-08 20:59 
AnswerRe: click event for custom tree control Pin
Cedric Moonen23-Nov-08 21:04
Cedric Moonen23-Nov-08 21:04 
GeneralRe: click event for custom tree control Pin
AnithaSubramani24-Nov-08 2:35
AnithaSubramani24-Nov-08 2:35 
Questionproblem with OnItemChange function Pin
T.SREENIVASA CHARY23-Nov-08 20:44
T.SREENIVASA CHARY23-Nov-08 20:44 
AnswerRe: problem with OnItemChange function Pin
Code-o-mat23-Nov-08 23:06
Code-o-mat23-Nov-08 23:06 
GeneralRe: problem with OnItemChange function Pin
T.SREENIVASA CHARY24-Nov-08 18:21
T.SREENIVASA CHARY24-Nov-08 18:21 
GeneralRe: problem with OnItemChange function Pin
Code-o-mat24-Nov-08 21:44
Code-o-mat24-Nov-08 21:44 
GeneralRe: problem with OnItemChange function Pin
T.SREENIVASA CHARY25-Nov-08 0:27
T.SREENIVASA CHARY25-Nov-08 0:27 
Questionchat control Pin
alphaxz23-Nov-08 20:34
alphaxz23-Nov-08 20:34 
AnswerRe: chat control Pin
Hamid_RT23-Nov-08 20:44
Hamid_RT23-Nov-08 20:44 
GeneralRe: chat control Pin
alphaxz23-Nov-08 21:11
alphaxz23-Nov-08 21:11 
GeneralRe: chat control Pin
Hamid_RT24-Nov-08 2:37
Hamid_RT24-Nov-08 2:37 
QuestionGet physical drive's information like model, serial number, type, etc. Pin
Shashi.Shinde23-Nov-08 20:18
Shashi.Shinde23-Nov-08 20:18 
AnswerRe: Get physical drive's information like model, serial number, type, etc. Pin
Hamid_RT23-Nov-08 20:23
Hamid_RT23-Nov-08 20:23 

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.