2017-10-18 47 views
0

我正在写一个恢复应用程序,从Chrome中提取密码。它有一个GUI,所以我使用了SQLite包装器,它使用SQLConnection和SQLQuery。这里是我的代码片段:为什么RAD Studio CreateBlobStream与CryptUnprotectData返回额外的字符?

//Create our blob stream 
TStream *Stream2 = SQLQuery1->CreateBlobStream(SQLQuery1->FieldByName("password_value"), bmRead); 
//Get our blob size 
int size = Stream2->Size; 
//Create our buffer 
char* pbDataInput = new char[size+1]; 
//Adding null terminator to buffer 
memset(pbDataInput, 0x00, sizeof(char)*(size+1)); 
//Write to our buffer 
Stream2->ReadBuffer(pbDataInput, size); 
DWORD cbDataInput = size; 

DataOut.pbData = pbDataInput; 
DataOut.cbData = cbDataInput; 

LPWSTR pDescrOut = NULL; 
//Decrypt password 
CryptUnprotectData(&DataOut, 
     &pDescrOut, 
     NULL, 
     NULL, 
     NULL, 
     0, 
     &DataVerify); 

//Output password 
UnicodeString password = (UnicodeString)(char*)DataVerify.pbData; 
passwordgrid->Cells[2][i] = password; 

输出数据看起来很好,除非它的行为就像我的null终止符出现问题一样。下面是输出看起来像每一行:

Output Data With Extra Chars at end of Password

我读过

的Windows文档为CryptUnprotectData

https://msdn.microsoft.com/en-us/library/windows/desktop/aa382377.aspx

Embarcadero的文档CreateBlobStream

http://docwiki.embarcadero.com/Libraries/en/Data.DB.TDataSet.CreateBlobStream

的memset:

http://www.cplusplus.com/reference/cstring/memset/

回答

1

您的阅读和解密电话上原始字节操作而已,他们一无所知的字符串,不关心他们。要添加到pbDataInput空终止从未使用过,所以摆脱它:

//Get our blob size 
int size = Stream2->Size; 
//Create our buffer 
char* pbDataInput = new char[size]; 
//Write to our buffer 
Stream2->ReadBuffer(pbDataInput, size); 
DWORD cbDataInput = size; 
... 
delete[] pbDataInput; 
delete Stream2; 

现在,分配pbDatapassword时,你是铸造pbDatachar*,所以UnicodeString构造解释数据作为以null结尾的ANSI字符串,并使用系统默认的ANSI代码页将其转换为UTF-16,这可能是非ASCII字符的有损转换。这是你真正想要的吗?

如果是这样,如果解密的数据实际上不是空值终止的,你必须指定的字符数为UnicodeString构造:

UnicodeString password((char*)DataVerify.pbData, DataVerify.cbData); 

在另一方面,如果解密输出已经在UTF-16,则需要投pbDatawchar_t*代替:

UnicodeString password = (wchar_t*)DataVerify.pbData; 

或者,如果不是空终止:

UnicodeString password((wchar_t*)DataVerify.pbData, DataVerify.cbData/sizeof(wchar_t)); 
+0

谢谢,我知道它与空终止符有关。提高你的答案。 –

相关问题