Click here to Skip to main content
15,886,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questioncalling a function multple times, clears previous values Pin
jkirkerx20-Dec-11 17:14
professionaljkirkerx20-Dec-11 17:14 
AnswerRe: calling a function multple times, clears previous values Pin
Richard MacCutchan20-Dec-11 21:46
mveRichard MacCutchan20-Dec-11 21:46 
GeneralRe: calling a function multple times, clears previous values Pin
jkirkerx21-Dec-11 6:16
professionaljkirkerx21-Dec-11 6:16 
QuestionRe: calling a function multple times, clears previous values Pin
David Crow21-Dec-11 2:38
David Crow21-Dec-11 2:38 
AnswerRe: calling a function multple times, clears previous values Pin
jkirkerx21-Dec-11 6:23
professionaljkirkerx21-Dec-11 6:23 
AnswerRe: calling a function multple times, clears previous values Pin
David Crow21-Dec-11 7:54
David Crow21-Dec-11 7:54 
GeneralRe: calling a function multple times, clears previous values Pin
jkirkerx21-Dec-11 8:15
professionaljkirkerx21-Dec-11 8:15 
GeneralMuch better now Pin
jkirkerx23-Dec-11 12:48
professionaljkirkerx23-Dec-11 12:48 
Thanks for your help. You were right on the money.

I learned alot on this one. Sort of like the post above this one. When you allocate memory, you have to have the exact right size including the null terminator.

So now I call it twice, once to get the size I need, and again with the right size.

I still have 1 bug left, I get 2 extra spaces in the SQL database column that are squares, I may have double null terminators or something. The -2 was just an experiment.

DWORD dwSecretAnwser;
caEncrypt._encrypt_3DES(szSecretAnwser, dwSecretAnwser);
WCHAR *szSecretAnwser_Secure = 	new WCHAR[dwSecretAnwser];
szSecretAnwser_Secure = caEncrypt._encrypt_3DES(szSecretAnwser, dwSecretAnwser);


WCHAR* CA_Encryption::_encrypt_3DES( WCHAR *pzInputW, DWORD &dwOutput )
{
	BOOL			bResult = FALSE;
	 
	HCRYPTPROV		hProv;
	HCRYPTKEY		hKey;
	ALG_ID			Algid;
	PBYTE			pbKeyBlob = NULL; 
    BYTE			*pbBuffer;
	DWORD			dwBlockLen = 0;
	DWORD			dwDataLen = 0;
	DWORD			cbData;
	BOOL			bFinal = TRUE;
	DWORD			errorCode = 0;
	TCHAR			*szBase64 = NULL;
	DWORD			dwResult;
	DWORD			cbKeyLen = sizeof(KEY_192);
	 
	// Get the size of the conversion first
	int iCharA = (WideCharToMultiByte(CP_UTF8, 0, pzInputW, -1, NULL, 0, NULL, NULL) - 1);	
	char *szInputA = new char[iCharA];
	WideCharToMultiByte(CP_UTF8, 0, pzInputW, -1, szInputA, iCharA, NULL, NULL);
		
	keyBlob192.hdr.bType = PLAINTEXTKEYBLOB;
	keyBlob192.hdr.bVersion = CUR_BLOB_VERSION;
	keyBlob192.hdr.reserved = 0;
	keyBlob192.hdr.aiKeyAlg = CALG_3DES;
	keyBlob192.cbKeySize = sizeof(KEY_192);
	CopyMemory(keyBlob192.rgbKeyData, KEY_192, cbKeyLen>  sizeof(keyBlob192) ? sizeof(keyBlob192) : cbKeyLen);
			
	bResult = CryptAcquireContextW( &hProv, 0, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
	bResult = CryptImportKey(hProv, (BYTE*)&keyBlob192, sizeof(keyBlob192), 0, CRYPT_EXPORTABLE, &hKey);	
	bResult = CryptSetKeyParam(hKey, KP_IV, IV_192, 0);
		
	dwDataLen = sizeof(ALG_ID);
	cbData = iCharA;

	if (!CryptGetKeyParam(hKey, KP_ALGID, (BYTE *)&Algid, &dwDataLen, 0)) 
		return 0;
	
	if (GET_ALG_TYPE(Algid) != ALG_TYPE_STREAM) {		
		dwDataLen = sizeof(DWORD);		
		if (!CryptGetKeyParam(hKey, KP_BLOCKLEN, (BYTE *)&dwBlockLen, &dwDataLen, 0))
		  return 0;		
		dwDataLen = ((cbData + dwBlockLen - 1) / dwBlockLen) * dwBlockLen;		
		if (!(pbBuffer = (BYTE *)LocalAlloc(LMEM_FIXED, dwDataLen))) 
			return 0;
		} 
		else {
			if (!(pbBuffer = (BYTE *)LocalAlloc(LMEM_FIXED, cbData))) 
				return 0;
		}	  
		CopyMemory(pbBuffer, szInputA, cbData);
		if (!CryptEncrypt(hKey, 0, bFinal, 0, pbBuffer, &cbData, dwDataLen)) {
			LocalFree(pbBuffer);
			return 0;
	}

	// Secure sensitive data in buffers
	SecureZeroMemory( keyBlob192.rgbKeyData, sizeof(keyBlob192.rgbKeyData) );
		
	// Clean up the garbage in the heap
	delete [] szInputA;
	CryptDestroyKey(hKey);
	CryptReleaseContext(hProv, 0);	

	if(CryptBinaryToString((BYTE*)pbBuffer, cbData, CRYPT_STRING_BASE64, NULL, &dwResult)) {		
		if  ((dwOutput > 0 ) && (dwOutput < 4096)) {
			szBase64 = new TCHAR[dwResult];
			CryptBinaryToString((BYTE*)pbBuffer, cbData, CRYPT_STRING_BASE64, szBase64, &dwResult);
			dwOutput = dwResult;
			szBase64[dwResult-2] = 0;
			return szBase64;
		}
		else {			
			dwOutput = dwResult;
			return L'\0';
		}				
	}
	else {
		dwOutput = dwResult;
		return L'\0';
	}	
}

Question16 bit C Compiler Pin
softwaremonkey19-Dec-11 23:40
softwaremonkey19-Dec-11 23:40 
AnswerRe: 16 bit C Compiler Pin
CPallini19-Dec-11 23:55
mveCPallini19-Dec-11 23:55 
AnswerRe: 16 bit C Compiler Pin
User 742933820-Dec-11 8:12
professionalUser 742933820-Dec-11 8:12 
GeneralRe: 16 bit C Compiler Pin
softwaremonkey20-Dec-11 8:23
softwaremonkey20-Dec-11 8:23 
GeneralRe: 16 bit C Compiler Pin
Richard MacCutchan20-Dec-11 21:40
mveRichard MacCutchan20-Dec-11 21:40 
GeneralRe: 16 bit C Compiler Pin
Richard Andrew x6422-Dec-11 10:24
professionalRichard Andrew x6422-Dec-11 10:24 
GeneralRe: 16 bit C Compiler Pin
softwaremonkey23-Dec-11 5:34
softwaremonkey23-Dec-11 5:34 
GeneralRe: 16 bit C Compiler Pin
Richard Andrew x6423-Dec-11 6:40
professionalRichard Andrew x6423-Dec-11 6:40 
AnswerRe: 16 bit C Compiler Pin
Software_Developer20-Dec-11 8:30
Software_Developer20-Dec-11 8:30 
GeneralRe: 16 bit C Compiler Pin
softwaremonkey23-Dec-11 5:57
softwaremonkey23-Dec-11 5:57 
QuestionDependencies for Visual STudio8 Pin
MKC00219-Dec-11 19:09
MKC00219-Dec-11 19:09 
AnswerRe: Dependencies for Visual STudio8 Pin
Richard Andrew x6419-Dec-11 20:24
professionalRichard Andrew x6419-Dec-11 20:24 
QuestionHow to capture snapshot of dialog in JPEG format? Pin
amitwadekar19-Dec-11 18:21
amitwadekar19-Dec-11 18:21 
AnswerRe: How to capture snapshot of dialog in JPEG format? Pin
Chandrasekharan P19-Dec-11 18:36
Chandrasekharan P19-Dec-11 18:36 
GeneralRe: How to capture snapshot of dialog in JPEG format? Pin
amitwadekar19-Dec-11 19:18
amitwadekar19-Dec-11 19:18 
AnswerRe: How to capture snapshot of dialog in JPEG format? Pin
PJ Arends19-Dec-11 21:43
professionalPJ Arends19-Dec-11 21:43 
GeneralRe: How to capture snapshot of dialog in JPEG format? Pin
amitwadekar25-Dec-11 19:52
amitwadekar25-Dec-11 19:52 

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.