我无法从 Windows 中类型为 CRED_TYPE_DOMAIN_PASSWORD 的凭证管理器读取密码( CredentialBlob ),我可以显示用户名,但对于显示密码( CredentialBlob )和大小 CredentialBlob 等于零则不返回任何内容。我使用 Visual Studio 2013。 我的阅读代码:
`
void credReadWindows(){
string targetName;
cout << "enter target name" << endl;
cin >> targetName;
LPCSTR targetNameLP = targetName.c_str();
DWORD flags = 0;
PCREDENTIALA readCred;
//CRED_TYPE_DOMAIN_PASSWORD
BOOL result = CredReadA(targetNameLP, CRED_TYPE_DOMAIN_PASSWORD, flags, &readCred);
if (result) {
string userName(readCred->UserName);
string password(reinterpret_cast<char*>(readCred->CredentialBlob),
static_cast<size_t>(readCred->CredentialBlobSize));
cout << "size CredentialBlob : " << sizeof(readCred->CredentialBlob) << endl;
cout << "size : " << readCred->CredentialBlobSize << endl;
cout << "Username: " << userName << endl;
cout << "Password: " << password << endl;
CredFree(readCred);
}
else {
DWORD errorCode = GetLastError();
cout << "Credential read failed with error code: " << errorCode << endl;
}
}
我用下面的代码编写凭据:
void credWriteWindows() {
string targetName ;
string userName ;
string password ;
string targetAlias;
cout << "enter target name :"<<endl;
cin >> targetName;
cout << "enter user name :" << endl;
cin >> userName;
cout << "enter password" << endl;
cin >> password;
cout << "enter target Alias:" << endl;
cin >> targetAlias;
CREDENTIALA writeCred = {};
writeCred.Flags = 0;
writeCred.Type = CRED_TYPE_DOMAIN_PASSWORD;
writeCred.TargetName = const_cast<char*>(targetName.c_str());
writeCred.CredentialBlobSize = static_cast<DWORD>(password.length());
writeCred.CredentialBlob = reinterpret_cast<LPBYTE>(const_cast<char*>(password.c_str()));
writeCred.Persist = CRED_PERSIST_LOCAL_MACHINE;
writeCred.UserName = const_cast<char*>(userName.c_str());
writeCred.TargetAlias = const_cast<char*>(targetAlias.c_str());
BOOL result = CredWriteA(&writeCred, 0);
if (result) {
cout << "Credential write successful" << endl;
}
else {
DWORD errorCode = GetLastError();
cout << "Credential write failed with error code: " << errorCode << endl;
}
}
来自文档(强调我的):
凭证Blob
凭证的秘密数据。 CredentialBlob 成员既可以读取也可以写入。
如果 Type 成员是 CRED_TYPE_DOMAIN_PASSWORD,则该成员包含 UserName 的明文 Unicode 密码。 CredentialBlob 和 CredentialBlobSize 成员不包含尾随零字符。 此外,对于 CRED_TYPE_DOMAIN_PASSWORD,该成员只能由身份验证包读取。