android - 使用密码学安全地存储数据

标签 android cryptography app-data

<分区>

背景

根据一篇新的 Android 开发人员博客文章(可用 here),您应该使用一种新技术来加密和解密存储在数据库或 sharedPreferences 中的敏感数据(例如密码),这样即使人们具有 root 权限的人将很难阅读它。

新的方法是:

public static SecretKey generateKey() throws NoSuchAlgorithmException {
    // Generate a 256-bit key
    final int outputKeyLength = 256;

    SecureRandom secureRandom = new SecureRandom();
    // Do *not* seed secureRandom! Automatically seeded from system entropy.
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(outputKeyLength, secureRandom);
    SecretKey key = keyGenerator.generateKey();
    return key;
}

问题

根据这篇文章(以及我的尝试),这段代码会在您每次调用它时创建一个新的不同 key ,因此我对此事有一些疑问:

  1. 应用程序可以为旧版 Android 做什么?

  2. 此类应用如何处理将 Android 操作系统更新到它应有的新工作方式(API post 17 到 API 17)?

  3. 由于它不是确定性的,这意味着解密无法重新创建相同的 key ,那么是否意味着还存储了该 key 以便将其用于解密(以及以后的加密)?这样的事情不会错过重点吗?

  4. 假设我有代码生成的 key ,我将如何使用它来加密和解密来自数据库和 sharedPreferences 的数据?

  5. 在文章中,他们说:

The fact is, Android's existing security model already provides plenty of protection for this kind of data. User credentials should be stored with the MODE_PRIVATE flag set and stored in internal storage

这是什么意思?不需要整个 key 生成,因为 Android 已经加密了所有内容?我的设备已获得 root 权限,我可以轻松地说数据从未加密并且易于阅读(尤其是 sharedPreferences,它只是 xml)。

最佳答案

what would apps do for older versions of android ?

不是这个,因为它在所有版本的 Android 上都没有用,正如博客文章所指出的。

since it's not deterministic, it means that decryption can't re-create the same key , so does it mean that the key is also stored in order to use it for decryption (and also later encryption) ? wouldn't such a thing miss the whole point ?

没错。引用博客文章:“请注意,这种方法的安全性依赖于保护生成的 key ,这是基于内部存储的安全性。不加密目标文件(但设置为 MODE_PRIVATE)将提供类似的安全性。”

老实说,我不知道为什么这篇博文是这样写的。唯一具有重要值(value)的加密形式是用户提供密码(或高级形式,例如双因素身份验证)。

what does it mean? that the whole key generation is not needed, as android already encrypt everything?

不,这意味着任何可以访问加密文件的人也可以访问加密 key ,这使得加密在很大程度上毫无意义。

请注意,Android 在用户已开启全盘加密的设备上“已经加密所有内容”。

关于android - 使用密码学安全地存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15046686/

相关文章:

android - 从 SharedPreferences 中删除条目 - Android

android - 在 Android Honeycomb 中更改菜单项文本颜色

asp.net - ASP.NET 中 APP_Data 文件夹的安全性

asp.net - 在Visual Studio中使用的App_Data文件夹是什么?

android - 生成签名 APK 向导生成启用调试的 APK

php - 如何使用 codeigniter 中的左连接 Activity 记录显示来自两个不同表的字段?

javascript - web crypto api - 如何导出 key 对?

.net - 如何为 TripleDESCryptoServiceProvider 生成强加密 key

c++ - 在 Windows 7 上使用 SHA2-512 (CALG_SHA_512) 返回 "Invalid Algorithm Specified"

facebook-c#-sdk - Facebook 网络表单应用程序获取 app_data 查询字符串