Click here to Skip to main content
15,888,337 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionwrite every data in new line through CFile Pin
AnithaSubramani25-Nov-08 22:44
AnithaSubramani25-Nov-08 22:44 
AnswerRe: write every data in new line through CFile Pin
SNI25-Nov-08 22:49
SNI25-Nov-08 22:49 
QuestionRe: write every data in new line through CFile Pin
CPallini25-Nov-08 23:24
mveCPallini25-Nov-08 23:24 
QuestionChanging the FileName Extension with saveas Dialog Pin
rkshdixit25-Nov-08 22:33
rkshdixit25-Nov-08 22:33 
QuestionMarquee Implementation Pin
Chandrasekharan P25-Nov-08 21:58
Chandrasekharan P25-Nov-08 21:58 
QuestionRe: Marquee Implementation Pin
CPallini25-Nov-08 23:18
mveCPallini25-Nov-08 23:18 
AnswerRe: Marquee Implementation Pin
Chandrasekharan P25-Nov-08 23:38
Chandrasekharan P25-Nov-08 23:38 
QuestionCryptDecrypt Failure please help Pin
monsieur_jj25-Nov-08 21:24
monsieur_jj25-Nov-08 21:24 
Hi all,

First of all props to Randor for helping me and giving me a base code now I modified his code and this is it:
#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <Wincrypt.h>
#include <Xenroll.h>
#include <atlenc.h>
#include <atlcrypt.h>

#pragma comment(lib, "crypt32.lib")//n7M41mbWvrA=
BYTE* Base64Decode2(LPCTSTR lpData, DWORD dwSize, DWORD *dwBufSize)
{
	DWORD dwResult = 0;
	if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
	{
		BYTE * decodedStr;
		decodedStr = new BYTE;
		LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
		memset(decodedStr,0,dwResult);
		if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,decodedStr, &dwResult,NULL,NULL))
		{
			*dwBufSize = dwResult;
			return decodedStr;
		}
	}
	return NULL;
}

LPTSTR Base64Encode2(BYTE* lpData, DWORD dwSize, BOOL bStripCRLF, DWORD *dwbufSize)
{
	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;
			}
			*dwbufSize = dwResult;
			return lpszBase64;
		}
	}
	return NULL;
}
DWORD TripleDESdecrypt2(TCHAR *cyphertext,DWORD ctlen,TCHAR *passwd,DWORD pwlen,BYTE *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);
	DWORD dMode = CRYPT_MODE_ECB;
        CryptSetKeyParam(hKey, PKCS5_PADDING, reinterpret_cast<const BYTE *>(&dMode), 0);
	if(*ptlen >= ctlen)
	{

		memcpy(plaintext,cyphertext,*ptlen);
		BOOL result = CryptDecrypt(hKey,NULL,1,0,plaintext,&ctlen);
		*ptlen=ctlen;
	}
	else
	{
		dwSizeNeeded = ctlen * sizeof(TCHAR);
	}
	CryptDestroyKey(hKey);
	CryptDestroyHash(hHash);
	CryptReleaseContext(hProv,0);
	return dwSizeNeeded;
}
int main()
{
	
	BYTE* decodedStr;
	HCRYPTPROV hCryptProv;
	HCRYPTHASH hHash = 0;
	HCRYPTKEY hKey = 0;

	TCHAR szKey[] = _T("h3bmull3r");
	TCHAR szUnencrypted[MAX_PATH * sizeof(TCHAR)] = _T("n7M41mbWvrA=");
	TCHAR * pszEncrypted = NULL;
	TCHAR * pszBase64 = NULL;
	TCHAR * pszBase64Decoded = NULL;
	TCHAR * psz3DESDecoded = NULL;
	DWORD dwSizeNeeded;
	DWORD BufSize;
	DWORD dwUnEncryptedSize;

	TCHAR * KeyBase64 = NULL;
	
	decodedStr = Base64Decode2(szUnencrypted,_tcslen(szUnencrypted) * sizeof(TCHAR), &BufSize);

	//Decrypt the string with 3DES algorithm using key
	dwUnEncryptedSize = NULL;
	TCHAR *decoded = reinterpret_cast<TCHAR*>(decodedStr);
	//dwSizeNeeded = BufSize;
	dwSizeNeeded = TripleDESdecrypt2(decoded,MAX_PATH * sizeof(TCHAR),szKey,_tcslen(szKey),decodedStr,&dwUnEncryptedSize);
	psz3DESDecoded = new TCHAR[dwSizeNeeded];
	TripleDESdecrypt2(decoded,dwSizeNeeded,szKey,_tcslen(szKey),decodedStr,&dwSizeNeeded);

	pszBase64 = Base64Encode2(decodedStr,BufSize,FALSE, &BufSize);

	delete [] pszBase64;
	delete [] pszBase64Decoded;
	delete [] pszEncrypted;

	return 0;
}


Now this is a long version of the decryption method of this:

public static string Decrypt(string cipherString, bool useHashing)
       {
           byte[] keyArray;
           byte[] toEncryptArray = Convert.FromBase64String(cipherString);

           System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
           //Get your key from config file to open the lock!
           string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

           if (useHashing)
           {
               MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
               keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
               hashmd5.Clear();
           }
           else
               keyArray = UTF8Encoding.UTF8.GetBytes(key);

           TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
           tdes.Key = keyArray;
           tdes.Mode = CipherMode.ECB;
           tdes.Padding = PaddingMode.PKCS7;


           ICryptoTransform cTransform = tdes.CreateDecryptor();
           byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

           tdes.Clear();
           return Convert.ToBase64String(resultArray, 0, resultArray.Length);
       }


Now I am able to encode a string to base 64 and get its original string bace by using Base64Encode2 and Base64Decode2. My goal now is to decrypt this string "n7M41mbWvrA=" into the string "test". Thing is CryptDecrypt fails and gives me a bad data. What could be possibly wrong with me.

Thanks,
Jayjay
QuestionRe: CryptDecrypt Failure please help Pin
CPallini25-Nov-08 21:39
mveCPallini25-Nov-08 21:39 
JokeRe: CryptDecrypt Failure please help Pin
Hamid_RT25-Nov-08 21:48
Hamid_RT25-Nov-08 21:48 
AnswerRe: CryptDecrypt Failure please help Pin
monsieur_jj25-Nov-08 22:00
monsieur_jj25-Nov-08 22:00 
GeneralRe: CryptDecrypt Failure please help Pin
CPallini25-Nov-08 22:32
mveCPallini25-Nov-08 22:32 
AnswerRe: CryptDecrypt Failure please help Pin
Randor 26-Nov-08 10:09
professional Randor 26-Nov-08 10:09 
GeneralRe: CryptDecrypt Failure please help Pin
monsieur_jj26-Nov-08 14:01
monsieur_jj26-Nov-08 14:01 
GeneralRe: CryptDecrypt Failure please help Pin
Randor 26-Nov-08 17:46
professional Randor 26-Nov-08 17:46 
GeneralRe: CryptDecrypt Failure please help Pin
monsieur_jj26-Nov-08 21:32
monsieur_jj26-Nov-08 21:32 
GeneralRe: CryptDecrypt Failure please help Pin
monsieur_jj27-Nov-08 19:37
monsieur_jj27-Nov-08 19:37 
GeneralRe: CryptDecrypt Failure please help Pin
Randor 28-Nov-08 12:59
professional Randor 28-Nov-08 12:59 
GeneralRe: CryptDecrypt Failure please help Pin
monsieur_jj29-Nov-08 3:52
monsieur_jj29-Nov-08 3:52 
GeneralRe: CryptDecrypt Failure please help Pin
Randor 29-Nov-08 16:47
professional Randor 29-Nov-08 16:47 
GeneralRe: CryptDecrypt Failure please help Pin
monsieur_jj30-Nov-08 3:44
monsieur_jj30-Nov-08 3:44 
Questionhow to display popup menu when clicked on a button on a dialog box? Pin
puppya25-Nov-08 20:26
puppya25-Nov-08 20:26 
AnswerRe: how to display popup menu when clicked on a button on a dialog box? Pin
SandipG 25-Nov-08 20:36
SandipG 25-Nov-08 20:36 
GeneralRe: how to display popup menu when clicked on a button on a dialog box? Pin
puppya25-Nov-08 21:05
puppya25-Nov-08 21:05 
AnswerRe: how to display popup menu when clicked on a button on a dialog box? Pin
Hamid_RT25-Nov-08 20:38
Hamid_RT25-Nov-08 20:38 

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.