Link to home
Start Free TrialLog in
Avatar of JoeD77
JoeD77

asked on

How to put the CryptDecrypt output into a string

Hey,
I'm using this code:
int decryptpwd(TCHAR *pwd) {
    HCRYPTPROV  phProv;
    HCRYPTHASH  phHash;
    HCRYPTKEY   phKey;
    DWORD       pwdlen;
    int         i;
pwdlen = strlen(pwd);

    if(!CryptAcquireContext(&phProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) return(-1);
    if(!CryptCreateHash(phProv, CALG_MD5, 0, 0, &phHash)) return(-1);
    if(!CryptHashData(phHash,  (BYTE*)L"MyKey", 16, 0)) return(-1);
    if(!CryptDeriveKey(phProv, CALG_RC4, phHash, 0x00800000, &phKey)) return(-1);
    if(!CryptDecrypt(phKey, 0, 1, 0, (BYTE*)pwd, &pwdlen)) return(-1);
    CryptDestroyKey(phKey);
    CryptDestroyHash(phHash);
    CryptReleaseContext(phProv, 0);
    return(0);
}

	int main()
{
		TCHAR pass[] = "D622C42F857CE95A3ADB8F4E3596";		
		decryptpwd(pass);
         size_t dwSize = strlen(pass);
		char* pChrRetVal = (char*)malloc(dwSize + 1);
               wprintf(L "- password: %s\n", (BYTE *)pass); ///WORKS! DISPLAYS FULL DECRYPTED PW
		wsprintf(pChrRetVal, "- password: %s\n", (BYTE *)pass); //DISPLAYS ONLY FIRST CHAR OF DECRYPTED PW
		cout << pChrRetVal;
  exit(0);
		
}

Open in new window


So I need to know how I can take the output of CryptDecrypt() and use the decrypted pass in a char buffer. As you can see I try to put the contents of (BYTE*)pass into a char buffer but it only displays the first character of the decrypted password.


Thanks!
Avatar of jkr
jkr
Flag of Germany image

The password is UNICODE, so just use 'wcout' instead of 'cout', e.g.
wcout << pChrRetVal;

Open in new window

Or, to do that in the 'tchar.h' manner, try
#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif

//...

		tout << pChrRetVal;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial