简体   繁体   English

如何在android中安全地保存密钥

[英]How to save secret key securely in android

I just read this article http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html where I learnt to generate security key. 我刚读了这篇文章http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html ,在那里我学会了生成安全密钥。

I want to know how to save this generated key securely so hackers wont get this even phone is rooted. 我想知道如何安全地保存这个生成的密钥,以便黑客不会得到这个甚至手机根植。

If we save this SharedPreference , Storage then hacker can get this. 如果我们保存这个SharedPreference ,则Storage然后黑客可以得到这个。

Thanks. 谢谢。

This is the overall problem with keeping access to the sensitive data. 这是保持对敏感数据的访问的总体问题。 There is always a way to decrypt, then the encryption key might leak. 总有一种方法可以解密,然后加密密钥可能会泄漏。

You might use EncryptedPreferences to store simple data in an encrypted way. 您可以使用EncryptedPreferences以加密方式存储简单数据。

However just a quick look into source code reveals, that you must pass a password on app init. 然而,只需快速查看源代码就会发现,您必须在app init上传递密码。

EncryptedPreferences encryptedPreferences = new EncryptedPreferences.Builder(this).withEncryptionPassword("password").build();

This is security leak, if the password is hardcoded. 如果密码是硬编码的,则这是安全漏洞。 This is not preferred method. 这不是优选的方法。

You might make use of the link you provided and generate a One-time pad . 您可以使用您提供的链接并生成一次性密码

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;
}

Of course an ideal situation is taken into account, where the key generating function is ideally random. 当然,考虑理想情况,其中密钥生成函数理想地是随机的。

Generate this key on first application start and use it in the library, which link I provided before. 在第一次应用程序启动时生成此密钥,并在库中使用它,这是我之前提供的链接。

Advantage: the key is different for each application installation. 优点:每个应用程序安装的密钥都不同。 That means if the cracker got to know the method how cipher works, he is still unable to decrypt other devices as long as he does not have an access to such device's SharedPreferences . 这意味着如果破解者知道密码如何工作的方法,他仍然无法解密其他设备,只要他无法访问此类设备的SharedPreferences

if Android is rooted , there is no way to secure any thing, so you should better look for architectural changes in your application. 如果Android是root用户 ,则无法保护任何内容,因此您最好在应用程序中查找体系结构更改。


Example : WhatsApp 示例:WhatsApp

Upon installation, WhatsApp creates a user account using one's phone number as the username (Jabber ID: [phone number]@s.whatsapp.net). 安装后,WhatsApp使用一个电话号码作为用户名创建一个用户帐户(Jabber ID:[电话号码] @s.whatsapp.net)。 A password is generated using an unknown algorithm on the server end and sent to the client. 密码在服务器端使用未知算法生成并发送到客户端。

But if phone is rooted you can easily extract this password as mention here . 但如果手机已植根,您可以轻松提取此密码,如此处所述

WhatsApp uses End-to-End Encryption, it stores all its data in encrypted form in internal storage. WhatsApp使用端到端加密,它将所有数据以加密形式存储在内部存储中。


Example : Snapchat 示例:Snapchat

Snapchat has stated that Snapchatters using a Rooted Android device will be blocked from logging in. Snapchat表示将阻止使用Rooted Android设备的Snapchatters登录。


Suggestion 建议

What you can do is to use the mixture of techniques by both giant applications WhatsApp and Snapchat ie 你可以做的是使用WhatsApp和Snapchat等巨型应用程序的混合技术

  • Block the phones that are rooted 阻止根植的手机
  • Make sure to make password "User-Specific" (every user has it's own key) rather than "App-specific" (the same on all devices) 确保密码“用户特定”(每个用户都有自己的密钥)而不是“特定于应用程序”(所有设备上都相同)
  • Save password on Server, and fetch it on every start of the application (validate and delete, do not store) 在服务器上保存密码,并在每次启动应用程序时获取密码(验证和删除,不存储)
  • Make sure all your data is in encrypted form 确保所有数据都是加密形式的

If you are generating and using the key in the application, it may be interesting to use the new (API 18+) Android Keystore Provider . 如果您在应用程序中生成并使用密钥,则使用新的(API 18+) Android密钥库提供程序可能会很有趣。 The key is stored by a special secure service, which may use secure hardware if available. 密钥由特殊的安全服务存储,如果可用,可以使用安全硬件。

It does not store an existing key (created elsewhere), but allow you to create and use keys without having access to the secret key itself. 它不存储现有密钥(在其他地方创建),但允许您创建和使用密钥,而无需访问密钥本身。 The idea is that the secret key never leaves the secure service, so that nobody can extract it, even your application (or root, if a secure hardware is used). 这个想法是秘密密钥永远不会离开安全服务,因此没有人可以提取它,甚至是你的应用程序(或root,如果使用安全硬件)。

It also allows you to put restriction on how the key is used (eg for a fixed duration after the user authentication) 它还允许您限制密钥的使用方式(例如,在用户身份验证后的固定时间内)

Root user has the permission to do anything on your android device. Root用户有权在您的Android设备上执行任何操作。 No matter where you save your generated key, a process running as root will be able to read it (as long as it knows where to read from). 无论您在何处保存生成的密钥,以root身份运行的进程都可以读取它(只要它知道从哪里读取)。 You may decide to encrypt the key before storing it, but then you have to determine where you will save the encryption key (again, if it's on the phone, root user can read it). 您可以决定在存储密钥之前加密密钥,但是您必须确定将保存加密密钥的位置(同样,如果它在手机上, root用户可以读取它)。

You may consider to ask the user of your app to provide the encryption key, and not store the encryption key on the device. 您可以考虑要求应用程序的用户提供加密密钥,而不是将加密密钥存储在设备上。 However, even then it may be possible to get hold of that encryption key given enough time and effort from an attacker. 但是,即使这样,在攻击者给予足够的时间和精力的情况下,也可以获得该加密密钥。

You should consider the requirements of your app, most probably, when the device is rooted your application should not provide any security guarantees to your users. 您应该考虑应用程序的要求,最有可能的是,当设备处于root状态时,您的应用程序不应向您的用户提供任何安全保证。 After all, there is a reason why rooting your device voids the warranty. 毕竟,有一个原因,为什么生根设备会使保修失效。

Rule one of security. 安全第一条。 Don't invent your own security. 不要发明自己的安全。 You can't create a way to store a private key safely on any device. 您无法创建在任何设备上安全存储私钥的方法。 When you've just learned to generate a key. 刚学会生成密钥的时候。

I just read this article http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html where I learnt to generate security key. 我刚读了这篇文章http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html ,在那里我学会了生成安全密钥。

A way that has already been invented is to make the user enter a string (something that is not saved on the phone) and use the the string for encryption. 已经发明的一种方法是让用户输入一个字符串(一些未保存在手机上的字符串)并使用该字符串进行加密。

The unsaved string method is easily broken by copying the ROM to a powerful machine and using brute force. 通过将ROM复制到功能强大的机器并使用强力,可以轻松破坏未保存的字符串方法。

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

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