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

N S*_*rma 17 security android

我刚读了这篇文章http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html,在那里我学会了生成安全密钥.

我想知道如何安全地保存这个生成的密钥,以便黑客不会得到这个甚至手机根植.

如果我们保存这个SharedPreference,Storage那么黑客可以得到这个.

谢谢.

R. *_*ski 14

这是保持对敏感数据的访问的总体问题.总有一种方法可以解密,然后加密密钥可能会泄漏.

您可以使用EncryptedPreferences以加密方式存储简单数据.

然而,只需快速查看源代码就会发现,您必须在app init上传递密码.

EncryptedPreferences encryptedPreferences = new EncryptedPreferences.Builder(this).withEncryptionPassword("password").build();
Run Code Online (Sandbox Code Playgroud)

如果密码是硬编码的,则这是安全漏洞.这不是优选的方法.

您可以使用您提供的链接并生成一次性密码.

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;
}
Run Code Online (Sandbox Code Playgroud)

当然,考虑理想情况,其中密钥生成函数理想地是随机的.

在第一次应用程序启动时生成此密钥,并在库中使用它,这是我之前提供的链接.

优点:每个应用程序安装的密钥都不同.这意味着如果破解者知道密码如何工作的方法,他仍然无法解密其他设备,只要他无法访问这样的设备SharedPreferences.