【发布时间】:2023-03-31 13:55:01
【问题描述】:
我们使用 Anroid Keystore 来存储一些机密数据并为 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);
}
}
【问题讨论】:
-
@Ketan,你有没有想过解决这个问题?
标签: android security xamarin xamarin.android android-keystore