JoeD77
asked on
How to put the CryptDecrypt output into a string
Hey,
I'm using this code:
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!
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);
}
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!
Or, to do that in the 'tchar.h' manner, try
#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif
//...
tout << pChrRetVal;
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Open in new window