Click here to Skip to main content
15,893,161 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: DES Encryption Pin
Randor 19-Dec-11 6:19
professional Randor 19-Dec-11 6:19 
GeneralRe: DES Encryption Pin
jkirkerx19-Dec-11 8:06
professionaljkirkerx19-Dec-11 8:06 
GeneralRe: DES Encryption Pin
Randor 19-Dec-11 8:29
professional Randor 19-Dec-11 8:29 
GeneralRe: DES Encryption Pin
jkirkerx19-Dec-11 8:47
professionaljkirkerx19-Dec-11 8:47 
GeneralRe: DES Encryption Pin
jkirkerx19-Dec-11 9:30
professionaljkirkerx19-Dec-11 9:30 
GeneralRe: DES Encryption Pin
Randor 19-Dec-11 10:36
professional Randor 19-Dec-11 10:36 
GeneralRe: DES Encryption Pin
jkirkerx19-Dec-11 10:53
professionaljkirkerx19-Dec-11 10:53 
GeneralPerfect Match! Pin
jkirkerx19-Dec-11 12:13
professionaljkirkerx19-Dec-11 12:13 
Perfect Match!, on the buffer value, and the base64 output. This is the framework for duplicating the asp.net DES and 3DES.

I need to clean it up, secure it, and take out the garbage to finalize it.

WOW, what a long journey, Thank you very much Dave for helping me out, and standing by my side during the journey, and not giving up on me.

// Link with the Advapi32.lib file.
#pragma comment (lib, "advapi32")
#pragma comment(lib, "crypt32.lib")
	
	BYTE DesKeyBlob_64[] = {
    0x08,0x02,0x00,0x00,0x01,0x66,0x00,0x00,	// BLOB header 
    0x08,0x00,0x00,0x00,						// key length, in bytes
    0x2a,0x10,0x5D,0x9C,0x4E,0x4,0xDA,0x20		// DES key with parity
    };	
		
WCHAR*	_encrypt_DES( WCHAR *pzInputW )
{
	BOOL			bResult = FALSE;
	WCHAR			*szOutput = NULL;
 
	HCRYPTPROV		hProv;
	HCRYPTKEY		hKey;
	ALG_ID			Algid;
	PBYTE			pbKeyBlob = NULL; 
    DWORD			dwKeyBlobLen;	
	BYTE			*pbBuffer;
	DWORD			dwBlockLen = 0;
	DWORD			dwDataLen = 0;
	DWORD			errorCode = 0;
	DWORD			cbData = 0;
	BOOL			bFinal = TRUE;
	 
	// 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);
			
	// Aquire a handle to the Encryption provider
	bResult = CryptAcquireContextW( &hProv, 0, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
	// hProv - Set the Key
	bResult = CryptImportKey(hProv, DesKeyBlob_64, sizeof(DesKeyBlob_64), 0, CRYPT_EXPORTABLE, &hKey);	
	// hProv - IV
	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;
	}

	// Peek inside the buffer before Binary to String
	BYTE szByte[8];
	for (int i = 0; i < cbData; ++i) {
		szByte[i] = (BYTE)pbBuffer[i];
	}				
	
	DWORD dwResult = 0;
	TCHAR *szBase64 = NULL;		
	if(CryptBinaryToString((BYTE*)pbBuffer, 8, CRYPT_STRING_BASE64, NULL, &dwResult)) {		
		szBase64 = new TCHAR[dwResult];		
		CryptBinaryToString((BYTE*)pbBuffer, 8, CRYPT_STRING_BASE64, szBase64, &dwResult);			
		szOutput = (WCHAR*)szBase64;
		delete [] szBase64;
	}	

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

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

GeneralRe: Perfect Match! Pin
Randor 19-Dec-11 14:03
professional Randor 19-Dec-11 14:03 
GeneralRe: Perfect Match! Pin
jkirkerx19-Dec-11 14:49
professionaljkirkerx19-Dec-11 14:49 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 7:59
professionaljkirkerx16-Dec-11 7:59 
AnswerSymmetric-key - Initialization Vector Pin
jkirkerx16-Dec-11 18:04
professionaljkirkerx16-Dec-11 18:04 
GeneralRe: Symmetric-key - Initialization Vector Pin
Chris Losinger17-Dec-11 9:38
professionalChris Losinger17-Dec-11 9:38 
QuestionInterview Preparation Pin
pix_programmer15-Dec-11 18:23
pix_programmer15-Dec-11 18:23 
AnswerRe: Interview Preparation Pin
Chandrasekharan P15-Dec-11 22:07
Chandrasekharan P15-Dec-11 22:07 
Questionmemory leaking in playing video using DirectShow. Pin
Le@rner14-Dec-11 23:48
Le@rner14-Dec-11 23:48 
QuestionRe: memory leaking in playing video using DirectShow. Pin
CPallini15-Dec-11 1:39
mveCPallini15-Dec-11 1:39 
AnswerRe: memory leaking in playing video using DirectShow. Pin
Le@rner15-Dec-11 17:47
Le@rner15-Dec-11 17:47 
QuestionConnecting to SQL CE from MFC Pocket PC 2003 Pin
Vieru Dorin14-Dec-11 21:10
Vieru Dorin14-Dec-11 21:10 
AnswerRe: Connecting to SQL CE from MFC Pocket PC 2003 Pin
Richard MacCutchan14-Dec-11 22:20
mveRichard MacCutchan14-Dec-11 22:20 
QuestionLibrary to solve optimization matrix Pin
qwerty_201114-Dec-11 16:49
qwerty_201114-Dec-11 16:49 
AnswerRe: Library to solve optimization matrix Pin
Stefan_Lang14-Dec-11 22:34
Stefan_Lang14-Dec-11 22:34 
GeneralRe: Library to solve optimization matrix Pin
qwerty_201115-Dec-11 16:30
qwerty_201115-Dec-11 16:30 
GeneralRe: Library to solve optimization matrix Pin
Stefan_Lang15-Dec-11 22:31
Stefan_Lang15-Dec-11 22:31 
QuestionUse the environment provided by the application instead from system environment. Pin
Member 848801414-Dec-11 16:12
Member 848801414-Dec-11 16:12 

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.