我们使用 Anroid Keystore 来存储一些 secret 数据并为 Keystore 设置密码。此密码与 Load、getKey 和 setKeyEntry 方法中的 KeyStore 类一起使用。
Keystore 本身是加密的,应用程序只能查看和查询自己的数据,所以我们可以说 Keystore 内部的数据有些安全,但是我们如何保护与 keystore 帐户关联的密码呢?我在网上找到了许多示例,其中大多数在代码中使用硬编码密码或使用空参数。
请参阅下面的示例。我想知道保护硬编码密码的最佳方法是什么?
想在 android 设备本身中找到一种安全的方式来存储这个硬编码的密码。假设将其移动到外部位置,如数据库、服务调用等选项不可用。
Context context;
KeyStore ks;
KeyStore.PasswordProtection prot;
static readonly object fileLock = new object ();
const string FileName = "Xamarin.Social.Accounts";
static readonly char[] Password = "3295043EA18CA264B2C40E0B72051DEF2D07AD2B4593F43DDDE1515A7EC32617".ToCharArray ();
public AndroidAccountStore (Context context)
{
this.context = context;
ks = KeyStore.GetInstance (KeyStore.DefaultType);
**prot = new KeyStore.PasswordProtection (Password);**
try {
lock (fileLock) {
using (var s = context.OpenFileInput (FileName)) {
ks.Load (s, Password);
}
}
}
catch (FileNotFoundException) {
//ks.Load (null, Password);
LoadEmptyKeyStore (Password);
}
}
最佳答案
Assume that moving it to external place like database, service call etc. is NOT possible
您希望将敏感信息安全地存储在本地用户的机器上。
唯一的方法就是加密它。最流行的加密算法是AES ,幸运的是微软在 C# 中包含了它的实现。
但是,加密使用 key 来加密/解密数据,因此我们基本上将问题移回 - 现在我们需要安全地存储该加密 key 。
您可以在应用程序中对该 key 进行硬编码,但专门的攻击者仍然可以获取它并解密密码。
相反,从用户那里获取该密码。要求他们提供密码,对其进行散列(使用例如 SHA256 )并使用散列作为加密 key 。
关于Android Keystore 的密码保护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782553/