【问题标题】:Android Keystore's password protectionAndroid Keystore 的密码保护
【发布时间】: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);
    }
}

【问题讨论】:

标签: android security xamarin xamarin.android android-keystore


【解决方案1】:

假设不可能将其移动到数据库、服务调用等外部位置

您希望将敏感信息安全地存储在本地用户的计算机上。 唯一的方法就是加密它。最流行的加密算法是AES,幸运的是微软在 C# 中包含了它的实现。

但是,加密使用密钥来加密/解密数据,因此我们基本上将问题移回 - 现在我们需要安全地存储该加密密钥。

您可以在应用程序中对该密钥进行硬编码,但专门的攻击者仍然可以获取它并解密密码。

相反,从用户那里获取该密码。要求他们提供密码,对其进行散列(使用例如SHA256)并使用散列作为加密密钥。

【讨论】:

猜你喜欢
  • 2017-03-25
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多