Click here to Skip to main content
15,907,183 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to set font of resources in multilingual application Pin
Waldermort18-Jan-07 20:59
Waldermort18-Jan-07 20:59 
AnswerRe: How to set font of resources in multilingual application Pin
John R. Shaw19-Jan-07 17:55
John R. Shaw19-Jan-07 17:55 
QuestionHow can solve the print preview problem Pin
Murugan k18-Jan-07 18:42
Murugan k18-Jan-07 18:42 
Questionencrypt and decrypt in UNICODE Pin
kiranin18-Jan-07 18:29
kiranin18-Jan-07 18:29 
QuestionRe: encrypt and decrypt in UNICODE Pin
Rajesh R Subramanian18-Jan-07 19:12
professionalRajesh R Subramanian18-Jan-07 19:12 
AnswerRe: encrypt and decrypt in UNICODE Pin
kiranin18-Jan-07 19:17
kiranin18-Jan-07 19:17 
QuestionRe: encrypt and decrypt in UNICODE Pin
Rajesh R Subramanian18-Jan-07 19:19
professionalRajesh R Subramanian18-Jan-07 19:19 
AnswerRe: encrypt and decrypt in UNICODE Pin
kiranin18-Jan-07 19:23
kiranin18-Jan-07 19:23 
BOOL EncryptString(LPTSTR szPassword,LPTSTR szEncryptPwd,LPTSTR szKey)
{	
	BOOL bResult = TRUE;	
	HKEY hRegKey = NULL;	
	HCRYPTPROV hProv = NULL;	
	HCRYPTKEY hKey = NULL;
	HCRYPTKEY hXchgKey = NULL;	
	HCRYPTHASH hHash = NULL;	
	DWORD dwLength;
	// Get handle to user default provider.
	if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))	
	{
		// Create hash object.		
		if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))		
		{
			// Hash password string.			
			dwLength = sizeof(TCHAR)*_tcslen(szKey);
			if (CryptHashData(hHash, (BYTE *)szKey, dwLength, 0))			
			{
				// Create block cipher session key based on hash of the password.
				if (CryptDeriveKey(hProv, MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))				
				{
					// Determine number of bytes to encrypt at a time.
					dwLength = sizeof(TCHAR)*_tcslen(szPassword);					
					// Allocate memory.
					BYTE *pbBuffer = (BYTE *)malloc(dwLength);					
					if (pbBuffer != NULL)					
					{
						memcpy(pbBuffer, szPassword, dwLength);						
						// Encrypt data
						if (CryptEncrypt(hKey, 0, TRUE, 0, pbBuffer, &dwLength, dwLength)) 						
						{
							// return encrypted string
							memcpy(szEncryptPwd, pbBuffer, dwLength);

						}	
						else						
						{							
							bResult = FALSE;						
						}						
						// Free memory
						free(pbBuffer);					
					}
					else					
					{						
						bResult = FALSE;					
					}
					CryptDestroyKey(hKey);  // Release provider handle.				
				}				
				else				
				{
					// Error during CryptDeriveKey!					
					bResult = FALSE;				
				}			
			}			
			else			
			{
				// Error during CryptHashData!				
				bResult = FALSE;			
			}
			CryptDestroyHash(hHash); 
			// Destroy session key.		
		}		
		else		
		{
			// Error during CryptCreateHash!			
			bResult = FALSE;		
		}
		CryptReleaseContext(hProv, 0);	
	}	
	return bResult;
}


BOOL DecryptString(LPTSTR szEncryptPwd,LPTSTR szPassword,LPTSTR szKey) 
{	
	BOOL bResult = TRUE;	
	HCRYPTPROV hProv = NULL;		
	HCRYPTKEY hKey = NULL;		
	HCRYPTKEY hXchgKey = NULL;
	HCRYPTHASH hHash = NULL;
	TCHAR szPasswordTemp[32] = _T("");
	DWORD dwLength;
	// Get handle to user default provider.
	if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))		
	{
		// Create hash object.			
		if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
		{				
			// Hash password string.
			dwLength = sizeof(TCHAR)*_tcslen(szKey);
			if (CryptHashData(hHash, (BYTE *)szKey, dwLength, 0))				
			{
				// Create block cipher session key based on hash of the password.
				if (CryptDeriveKey(
					hProv, MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))					
				{
					// we know the encrypted password and the length
					dwLength = sizeof(TCHAR)*_tcslen(szEncryptPwd);						
					// copy encrypted password to temporary TCHAR
					_tcscpy(szPasswordTemp,szEncryptPwd);
					if (!CryptDecrypt(
							hKey, 0, TRUE, 0, (BYTE *)szPasswordTemp, &dwLength))
						bResult = FALSE;						
					CryptDestroyKey(hKey);  // Release provider handle.					
					// copy decrypted password to outparameter
					_tcscpy(szPassword,szPasswordTemp);
				}					
				else					
				{
					// Error during CryptDeriveKey!						
					bResult = FALSE;					
				}				
			}				
			else
			{					
				// Error during CryptHashData!					
				bResult = FALSE;				
			}
			CryptDestroyHash(hHash); // Destroy session key.			
		}			
		else			
		{
			// Error during CryptCreateHash!				
			bResult = FALSE;			
		}
		CryptReleaseContext(hProv, 0);		
	}		
	return bResult;
}

These are the functions am using to encrypt and decrypt. These are working fine when i define _MBCS, but if UNICODE deifned the functions are not working
GeneralRe: encrypt and decrypt in UNICODE Pin
Rajesh R Subramanian18-Jan-07 19:30
professionalRajesh R Subramanian18-Jan-07 19:30 
GeneralRe: encrypt and decrypt in UNICODE Pin
kiranin18-Jan-07 19:35
kiranin18-Jan-07 19:35 
GeneralRe: encrypt and decrypt in UNICODE Pin
Rajesh R Subramanian18-Jan-07 19:57
professionalRajesh R Subramanian18-Jan-07 19:57 
GeneralRe: encrypt and decrypt in UNICODE Pin
kiranin18-Jan-07 20:04
kiranin18-Jan-07 20:04 
GeneralRe: encrypt and decrypt in UNICODE Pin
Rajesh R Subramanian18-Jan-07 20:07
professionalRajesh R Subramanian18-Jan-07 20:07 
GeneralRe: encrypt and decrypt in UNICODE Pin
Mark Salsbery19-Jan-07 5:59
Mark Salsbery19-Jan-07 5:59 
GeneralRe: encrypt and decrypt in UNICODE Pin
Mark Salsbery19-Jan-07 6:06
Mark Salsbery19-Jan-07 6:06 
GeneralRe: encrypt and decrypt in UNICODE Pin
Mark Salsbery19-Jan-07 6:19
Mark Salsbery19-Jan-07 6:19 
QuestionUSB Drive Pin
radhika2818-Jan-07 17:57
radhika2818-Jan-07 17:57 
AnswerRe: USB Drive Pin
Michael Dunn18-Jan-07 19:47
sitebuilderMichael Dunn18-Jan-07 19:47 
Questioncmd.exe bug / GetFileType from kernel mode Pin
Mike_V18-Jan-07 16:20
Mike_V18-Jan-07 16:20 
Questionhelp returning data from DLL Pin
Calvin Streeting18-Jan-07 13:36
Calvin Streeting18-Jan-07 13:36 
QuestionRe: help returning data from DLL Pin
prasad_som18-Jan-07 17:30
prasad_som18-Jan-07 17:30 
AnswerRe: help returning data from DLL Pin
Calvin Streeting18-Jan-07 22:00
Calvin Streeting18-Jan-07 22:00 
QuestionProblem with my code - cant fine what wrong - please need help. Pin
Yanshof18-Jan-07 12:12
Yanshof18-Jan-07 12:12 
AnswerRe: Problem with my code - cant fine what wrong - please need help. Pin
Stephen Hewitt18-Jan-07 12:17
Stephen Hewitt18-Jan-07 12:17 
GeneralRe: Problem with my code - cant fine what wrong - please need help. Pin
prasad_som18-Jan-07 17:33
prasad_som18-Jan-07 17:33 

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.