我想使用 存储和检索密码Windows Hello。用户可以在登录时选择是否要手动输入密码,或者是否要使用Windows Hello解锁(然后检索上次使用的密码,并为用户填写)。
如果Windows Hello设置正确,文档中有两个用例。
只需解锁即可:
UserConsentVerificationResult consentResult = await UserConsentVerifier.RequestVerificationAsync("userMessage");
if (consentResult.Equals(UserConsentVerificationResult.Verified))
{
// continue
}
Run Code Online (Sandbox Code Playgroud)
以及对来自服务器的消息进行签名的方法:
var openKeyResult = await KeyCredentialManager.OpenAsync(AccountId);
if (openKeyResult.Status == KeyCredentialStatus.Success)
{
var userKey = openKeyResult.Credential;
var publicKey = userKey.RetrievePublicKey();
//the message is the challenge from the server
var signResult = await userKey.RequestSignAsync(message);
if (signResult.Status == KeyCredentialStatus.Success)
{
//the with the private key of the user signed message
return signResult.Result;
}
}
Run Code Online (Sandbox Code Playgroud)
两者对于我的用例来说都不是很有用:我想要一种对称的方式来存储和检索密码。
简而言之,我的问题是:
有没有办法对称存储数据Windows Hello?
相关文档:
https://learn.microsoft.com/en-us/windows/uwp/security/microsoft-passport
我通过使用 Windows Hello 生成的密码来加密/解密我想要存储的秘密,解决了这个问题。密码是固定消息的签名。
一个完整的代码示例(未经测试)来说明我的观点:
const accountId = "A ID for this key";
const challenge = "sign this challenge using Windows Hello to get a secure string";
public async Task<byte[]> GetSecureEncryptionKey() {
// if first time; we first need to create a key
if (firstTime) {
if (!await this.CreateKeyAsync()) {
return null;
}
}
// get the key using Windows Hellp
return await this.GetKeyAsync();
}
private async Task<bool> CreateKeyAsync() {
if (!await KeyCredentialManager.IsSupportedAsync()) {
return false;
}
// if app opened for the first time, we need to create an account first
var keyCreationResult = await KeyCredentialManager.RequestCreateAsync(AccountId, KeyCredentialCreationOption.ReplaceExisting);
return keyCreationResult.Status == KeyCredentialStatus.Success);
}
private async Task<byte[]> GetKeyAsync() {
var openKeyResult = await KeyCredentialManager.OpenAsync(AccountId);
if (openKeyResult.Status == KeyCredentialStatus.Success)
{
// convert our string challenge to be able to sign it
var buffer = CryptographicBuffer.ConvertStringToBinary(
challenge, BinaryStringEncoding.Utf8
);
// request a sign from the user
var signResult = await openKeyResult.Credential.RequestSignAsync(buffer);
// if successful, we can use that signature as a key
if (signResult.Status == KeyCredentialStatus.Success)
{
return signResult.Result.ToArray();
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
完整的源代码位于github 上,并展示了我如何将这些概念集成到应用程序中。
然而,这滥用了用于不同目的的密码原语,这是非常危险的。在采用此解决方法之前,请考虑寻找更合理的方法。
具体注意事项:
key = sha256(signature);| 归档时间: |
|
| 查看次数: |
1608 次 |
| 最近记录: |