CryptUnprotectData keep failing with error n 13

i've this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::string Database::Decrypt(std::string line)
{
	DATA_BLOB ClearText;
	ZeroMemory(&ClearText,sizeof(ClearText));
	DATA_BLOB EncryptedData;
	ZeroMemory(&EncryptedData,sizeof(EncryptedData));

	EncryptedData.cbData = line.length() +1;
	EncryptedData.pbData = (BYTE*) line.c_str();

	if(CryptUnprotectData(&EncryptedData,NULL,NULL,NULL,NULL,0,&ClearText) == false)
	{
		std::cerr << "Decrypt() fail with number: " << GetLastError() << std::endl;
		system("pause");
		LocalFree(&ClearText);
		LocalFree(&EncryptedData);
		exit(-1);
	}
	std::string tmp(reinterpret_cast<const char*>(ClearText.pbData));
	return tmp;
}

CryptUnprotectData keep failing with error n 13 and i'm not able to solve this.
There's a way around?
No one can help me?
Lookt at the example here:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa380882%28v=vs.85%29.aspx

The second parameter requires a pointer to LPWSTR which can be set to NULL:
1
2
3
4
5
6
7
8
9
10
11
LPWSTR pDescrOut =  NULL;
//--------------------------------------------------------------------
// The buffer DataOut would be created using the CryptProtectData
// function. If may have been read in from a file.

//--------------------------------------------------------------------
//   Begin unprotect phase.

if (CryptUnprotectData(
        &DataOut,
        &pDescrOut,
i've changed the function into this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
std::string Database::Decrypt(std::string line)
{
	DATA_BLOB ClearText;
	ZeroMemory(&ClearText,sizeof(ClearText));
	DATA_BLOB EncryptedData;
	ZeroMemory(&EncryptedData,sizeof(EncryptedData));

        LPWSTR pDescrOut =  NULL;

	EncryptedData.cbData = line.length() +1;
	EncryptedData.pbData = (BYTE*) line.c_str();

	if(CryptUnprotectData(&EncryptedData,&pDescrOut ,NULL,NULL,NULL,0,&ClearText) == false)
	{
		std::cerr << "Decrypt() fail with number: " << GetLastError() << std::endl;
		system("pause");
		LocalFree(&ClearText);
		LocalFree(&EncryptedData);
		exit(-1);
	}
	std::string tmp(reinterpret_cast<const char*>(ClearText.pbData));
	return tmp;
}


but it still fail with error 13
Wait, you're using CryptUnprotectData with the not encrypted data line. Yes, that fails. Use CryptProtectData(...) before this:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa382377%28v=vs.85%29.aspx
No,line is encrypted. I need to decrypt it. I read a file and store it in std::string line; and then
i pass line variable to std::string Database::Decrypt(std::string line) that should return the decrypted line
help?
Well, the decryption fails if a single byte is wrong. So I'm surprised that the encrypted data is a string. How did the string get the data? Does the terminating 0 belong to the encrypted data?

Make sure that each and every byte (not more not less) from the encryption is passed to the decryption.

By the way: line 17/18 are wrong. ClearText and EncryptedData are not dynamically allocated hence you shouldn't use LocalFree.

while ClearText.pbData should be used with LocalFree
Topic archived. No new replies allowed.