简体   繁体   English

Android安全存储

[英]Android Secure Storage

I want to store some small but critical piece of information such as AES keys in my Android application. 我想在我的Android应用程序中存储一些小而重要的信息,例如AES密钥。 What would be the recommended way to do this? 建议的方法是什么? I do not want to hardcode keys as part of my application. 我不想将密钥硬编码作为我的应用程序的一部分。

I look at KeyStore but it does not really solve my problem. 我看看KeyStore,但它并没有真正解决我的问题。 It can store my keys given that I can provide a password. 它可以存储我的密钥,因为我可以提供密码。 Then I need to find a secure place to store this password which is same as my original problem. 然后我需要找到一个安全的地方来存储这个密码,这与我原来的问题相同。

Is there a built in Android class to perform this task? 是否有内置的Android类来执行此任务? Or should I look for third party libraries? 或者我应该寻找第三方图书馆? Using NDK is also acceptable for me. 使用NDK对我来说也是可以接受的。

Update: 更新:

I was hoping to find an Android API for storage such that guarantees that only the application that stored some information can retrieve it back. 我希望找到一个用于存储的Android API,以确保只有存储某些信息的应用程序才能检索回来。 Android OS could have enforced this based on signing signatures of the application. Android OS可以基于签署应用程序的签名来强制执行此操作。 This way my application can generate a random key on first run and store it in secure storage for later use. 这样,我的应用程序可以在第一次运行时生成随机密钥,并将其存储在安全存储中以供以后使用。 Are there any API for this? 这有什么API吗?

Is there a built in Android class to perform this task? 是否有内置的Android类来执行此任务?

Other than java.io.File , no. 除了java.io.File ,没有。

Or should I look for third party libraries? 或者我应该寻找第三方图书馆?

You can try, but I suspect most will look like the solution you already rejected. 你可以尝试,但我怀疑大多数看起来像你已经拒绝的解决方案。 Most secure data stores involve passwords and assume the passwords are held elsewhere (eg, in a user's head). 大多数安全数据存储涉及密码并假设密码保存在其他地方(例如,在用户的头部)。 For example, OI Safe has an Intent-based system of allowing applications to store stuff in the safe, but then the user is involved in unlocking the safe, IIRC. 例如, OI Safe有一个基于Intent的系统,允许应用程序将东西存储在保险箱中,然后用户参与解锁保险箱IIRC。

One solution, if you end up with using the KeyStore API, is to generate your password dynamically at run time every time the app needs to access the KeyStore. 如果最终使用KeyStore API,一种解决方案是在每次应用程序需要访问KeyStore时在运行时动态生成密码。 If you base your password algorithm on a simple, but changeable, variable tied to the specific installation such as the device MEID (or other specific ID of the physical device gained at run time) you could provide a key to the lock that becomes increasingly difficult to pick. 如果您将密码算法基于一个简单但可更改的变量,该变量与特定安装相关联,例如设备MEID(或运行时获得的物理设备的其他特定ID),您可以提供锁定变得越来越困难的密钥选择。

Example: use an ID from the physical device, cut in three positions and append them to the end position in the ID string, then append your initials to the string programmatically. 示例:使用物理设备中的ID,切入三个位置并将它们附加到ID字符串中的结束位置,然后以编程方式将首字母附加到字符串。 I would think this approach would give a layer of security that cannot be easily broken unless the cracker knows how you made the key (ie has your source code). 我认为这种方法会给出一层不容易破解的安全性,除非破解者知道你是如何制作密钥的(即有你的源代码)。

MEID = MEID + "fluffy" + "2008";

Where MEID is a string with some ID from the device, "fluffy" is the name of your best friends cat, and "2008" is the year of an important event in your life. MEID是一个带有设备ID的字符串,“蓬松”是你最好的朋友猫的名字,“2008”是你生命中重要事件的一年。 Then feed this new string into an array, parse through a number that suits you (the day of the month that you got your drivers licence for example), grab three chars out and drop those chars at the end of the string. 然后将这个新字符串提供给一个数组,解析一个适合你的数字(例如,你获得驾驶执照的月份),抓住三个字符并将字符串放在字符串的末尾。 Clip from the front of the string to the number of positions you need for your key and away you go. 从字符串的前面剪切到您需要的键位数,然后离开。 This should not be a very processor intensive task so, with some fault tolerance code for the variables, you should be able to run this in your main process even with out too much worry of getting an ANR from the system. 这不应该是一个非常耗费处理器的任务,因此,对于变量的一些容错代码,您应该能够在主进程中运行它,即使过分担心从系统获取ANR也是如此。 If you really want to get froggy, convert the string to bits at some point and 'bitwise op' the changes. 如果你真的想得到蛙式,请在某个时刻将字符串转换为位,然后按位“按位”转换。 Viola, a low overhead, dynamic key that is unique to the device it is run on! Viola,一种低开销的动态密钥,对于运行它的设备是独一无二的!

EDIT: 编辑:

As @RedWarp pointed out, decompiling an .apk is always within the realm of possiblility for any object code with the proper tools and motivation. 正如@RedWarp指出的那样,反编译.apk总是在任何具有适当工具和动机的目标代码的可能性范围内。 If the "key" generation is a really important process then abstracting the key gen outside the scope of the app is a must. 如果“密钥”生成是一个非常重要的过程,那么在应用程序范围之外抽象密钥gen是必须的。

The real point that I am trying to make with this answer is that a little fore thought can go a ways with regards to minimal security. 我试图用这个答案做的真正观点是,一点点预想可以在最小的安全性方面采取措施。 Stronger security is more in depth than a simple answer from me. 更强大的安全性比我简单的答案更深入。

You have to distinguish here between keys and app data. 您必须在此区分键和应用数据。

The "AndroidKeyStore" KeyPairGenerator and KeyGenerator stores the keys they generate from your app under an alias in the KeyStore and associates the keys to your app. “AndroidKeyStore”KeyPairGenerator和KeyGenerator将您在应用程序中生成的密钥存储在KeyStore中的别名下,并将密钥与您的应用程序相关联。 If the device has "secure hardware" you can specify that it be used and the keys will be stored there. 如果设备具有“安全硬件”,则可以指定使用它并将密钥存储在那里。

There is no password for the keys. 密钥没有密码。 You use the alias to specify which key you want to use. 您可以使用别名指定要使用的密钥。 Only your app can retrieve the keys it had generated. 只有您的应用可以检索它生成的密钥。

see: https://developer.android.com/training/articles/keystore.html 请参阅: https//developer.android.com/training/articles/keystore.html

For your app's private data, if I understand ".. an Android API for storage such that guarantees that only the application that stored some information can retrieve it back. ..", you might look at these: 对于您应用的私人数据,如果我理解“......用于存储的Android API,以确保只有存储某些信息的应用程序可以将其检索回来......”,您可能会看到以下内容:

https://developer.android.com/guide/topics/data/data-storage.html#filesInternal https://developer.android.com/guide/topics/data/data-storage.html#db https://developer.android.com/guide/topics/data/data-storage.html#filesInternal https://developer.android.com/guide/topics/data/data-storage.html#db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM