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

C / C++ / MFC

 
Questionabout DLL dependencies Pin
ryan218921-Dec-11 5:37
ryan218921-Dec-11 5:37 
AnswerRe: about DLL dependencies Pin
Richard Andrew x6422-Dec-11 10:10
professionalRichard Andrew x6422-Dec-11 10:10 
AnswerRe: about DLL dependencies Pin
Lactoferrin22-Dec-11 21:14
Lactoferrin22-Dec-11 21:14 
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 
I just finished these,

Encrypt 3DES

WCHAR*	CA_Encryption::_encrypt_3DES( WCHAR *pzInputW )
{
BOOL			bResult = FALSE;
WCHAR			*szOutput = NULL;
 
HCRYPTPROV		hProv;
HCRYPTKEY		hKey;
ALG_ID			Algid;
PBYTE			pbKeyBlob = NULL; 
BYTE			*pbBuffer;
DWORD			dwBlockLen = 0;
DWORD			dwDataLen = 0;
DWORD			cbData = 0;
BOOL			bFinal = TRUE;
DWORD			errorCode = 0;

DWORD			dwResult = 0;
TCHAR			szBase64[80];
DWORD			cbKeyLen = 24;
	 
// Get the size of the conversion first
int iCharA = WideCharToMultiByte(CP_UTF8, 0, pzInputW, -1, NULL, 0, NULL, NULL);	
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 = 24;
ZeroMemory(keyBlob192.rgbKeyData, 24);
CopyMemory(keyBlob192.rgbKeyData, KEY_192, cbKeyLen > 24 ? 24 : 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_64, 0);
		
dwDataLen = sizeof(ALG_ID);
cbData = sizeof(szInputA);	

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)) {
		DWORD dwError = GetLastError();		
		if(ERROR_MORE_DATA == dwError) {
			DWORD dwSizeNeeded = cbData;
			CopyMemory( pbBuffer, szInputA, cbData );
			bResult = CryptEncrypt(hKey, 0, FALSE, 0, pbBuffer, &dwSizeNeeded, dwDataLen);
		}			
		LocalFree(pbBuffer);
		return 0;
}	
	
if(CryptBinaryToString(pbBuffer, 8, CRYPT_STRING_BASE64, NULL, &dwResult)) {		
	CryptBinaryToString(pbBuffer, 8, CRYPT_STRING_BASE64, szBase64, &dwResult);
	szBase64[dwResult] = 0;
	szOutput = (WCHAR*)szBase64;
	szOutput[wcslen(szOutput)+1] = L'\0';
}	

// Secure sensitive data in buffers
SecureZeroMemory( keyBlob64.rgbKeyData, 8 );
SecureZeroMemory( pbBuffer, sizeof(pbBuffer) );
	
// Clean up the garbage in the heap
delete [] szInputA;

CryptDestroyKey(hKey);
CryptReleaseContext(hProv, 0);	
	
return szOutput;
}


Hash

BYTE* CA_Encryption::_create_MD5_Hash( WCHAR *pzInputW )
{
BOOL			bResult = FALSE;
HCRYPTPROV		hProv;
HCRYPTHASH		hHash;

const int		MD5_SIZE = 16;
static BYTE		hash[MD5_SIZE];
DWORD			hash_size = sizeof(hash);	
			
// Get the size of the conversion
int iChar = WideCharToMultiByte(CP_UTF8, 0, pzInputW, -1, NULL, 0, NULL, NULL);	
char *szInputA = new char[iChar];
WideCharToMultiByte(CP_UTF8, 0, pzInputW, -1, szInputA, iChar, NULL, NULL);
		
DWORD		dwBufferSize = 0;	
DWORD		dwPasswordLen = strlen((char *)szInputA);	
		
bResult = CryptAcquireContextW( &hProv, NULL, MS_STRONG_PROV, PROV_RSA_FULL, 0);	
bResult = CryptCreateHash( hProv, CALG_MD5, 0, 0, &hHash );   
bResult = CryptHashData( hHash, (BYTE*)szInputA, dwPasswordLen, 0 );
	
// Now get the value
bResult = CryptGetHashParam( hHash, HP_HASHVAL, hash, &hash_size, 0 );
		
// Release hash object.
delete [] szInputA;
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
		    	
return hash;
}

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 
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 

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.