CryptUnprotectData append LMEM to plain string

hi,
i'm using CryptUnprotectData to decrypt a value in a sqlite3's column crypted with CryptProtectData.
The decryption works,but when i display the string the program show plain string + random char + "LMEM"

the function is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
std::vector<std::string> Decrypt(sqlite3_stmt * stmt,int column)
 {
	 LPWSTR str = NULL;
	 
	 std::vector<std::string> data;
	 std::string tmp;

	 DATA_BLOB in,out;
	 memset(&in,0,sizeof(in));
	 memset(&out,0,sizeof(out));
	 
	 try
	 {
		 while(sqlite3_step(stmt) == SQLITE_ROW)
		 {

			 in.cbData = sqlite3_column_bytes(stmt,column);
			 in.pbData = (BYTE *)sqlite3_column_blob(stmt,column);

			 if(!CryptUnprotectData(&in,&str,NULL,NULL,NULL,0,&out))
			 {
				 throw chrome::Event::Err(GetLastError());
			 }

			 tmp = reinterpret_cast<char*>(out.pbData);
			 data.push_back(tmp);



			 std::cout << ".";
		 }

		 return data;
	 }
	 catch(chrome::Event::Err& exc)
	 {
		 std::cerr << "CryptUnprotectData() fail " << GetLastError() << std::endl;
		 LocalFree(str);
		 LocalFree(&in);
		 LocalFree(&out);
		 data.at(0) = "ERROR";
		 return data;
	 }


 }


Thanks to who'll answer
I've seen the example ,but it doesn't answer my question...
none can help?
The reason is that on line 25 out.pbData does not have a terminating 0. You need out.cbData in order to determine the correct string length
How can i null-terminate a byte* string with the STL?
Last edited on
like so:

tmp.assign(reinterpret_cast<char*>(out.pbData), out.cbData);
Registered users can post here. Sign in or register to post.