Most app developers will integrate some third party libraries into their apps. If it's to access a service, such as Dropbox or YouTube, or for logging crashes. The number of third party libraries and services is staggering. Most of those libraries and services are integrated by somehow authenticating with the service, most of the time, this happens through an API key. For security purposes, services usually generate a public and private, often also referred to as secret, key. Unfortunately, in order to connect to the services, this private key must be used to authenticate and hence, probably be part of the application. Needless to say, that this faces in immense security problem. Public and private API keys can be extracted from APKs in a matter of minutes and can easily be automated.

假设我有类似的东西,我如何保护密钥:

public class DropboxService  {

    private final static String APP_KEY = "jk433g34hg3";
    private final static String APP_SECRET = "987dwdqwdqw90";
    private final static AccessType ACCESS_TYPE = AccessType.DROPBOX;

    // SOME MORE CODE HERE

}

你认为储存私钥的最佳及最安全的方法是什么?混淆,加密,你怎么看?


当前回答

保持这些隐私的唯一方法是把它们保存在你的服务器上,让应用程序把它们发送到服务器上,然后服务器与Dropbox交互。这样你就不会以任何格式分发你的私钥。

其他回答

旧的不安全的方式:

遵循3个简单的步骤来保护API/密钥(旧答案)

我们可以使用Gradle来保护API密钥或秘密密钥。

1. gradle。properties(项目属性):创建带有key的变量。

GoogleAPIKey = "Your API/Secret Key"

2. 构建。gradle (Module: app):在build中设置变量。Gradle在活动或片段中访问它。将以下代码添加到buildTypes{}。

buildTypes.each {
    it.buildConfigField 'String', 'GoogleSecAPIKEY', GoolgeAPIKey
}

3.通过应用程序的BuildConfig在Activity/Fragment中访问它:

BuildConfig.GoogleSecAPIKEY

更新:

上述解决方案对通过Git提交的开源项目很有帮助。(感谢David Rawson和riyazi -ali的评论)。

根据Matthew和Pablo Cegarra的评论,上述方式是不安全的,反编译器将允许某人使用我们的密钥查看BuildConfig。

解决方案:

我们可以使用NDK来保护API密钥。我们可以在原生C/ c++类中存储键,并在Java类中访问它们。

请跟随这个博客使用NDK保护API密钥。

关于如何在Android中安全地存储令牌的后续内容

另一种方法是一开始就不要在设备上保存秘密!请参阅移动API安全技术(特别是第3部分)。

使用历史悠久的间接传统,分享API端点和应用程序身份验证服务之间的秘密。

当您的客户端想要进行API调用时,它要求应用程序认证服务对其进行身份验证(使用强大的远程认证技术),并接收由秘密签名的时间限制令牌(通常是JWT)。

令牌随每个API调用一起发送,端点可以在对请求执行之前验证其签名。

真正的秘密永远不会出现在设备上;事实上,应用程序从来不知道它是否有效,它只是请求身份验证并传递结果令牌。间接的好处是,如果你想改变秘密,你可以不需要用户更新他们安装的应用程序。

所以如果你想保护你的秘密,从一开始就不要把它放在你的应用程序中是一个很好的方法。

保持这些隐私的唯一方法是把它们保存在你的服务器上,让应用程序把它们发送到服务器上,然后服务器与Dropbox交互。这样你就不会以任何格式分发你的私钥。

Whatever you do to secure your secret keys is not going to be a real solution. If developer can decompile the application there is no way to secure the key, hiding the key is just security by obscurity and so is code obfuscation. Problem with securing a secret key is that in order to secure it you have to use another key and that key needs to also be secured. Think of a key hidden in a box that is locked with a key. You place a box inside a room and lock the room. You are left with another key to secure. And that key is still going to be hardcoded inside your application.

因此,除非用户输入PIN或短语,否则无法隐藏密钥。但要做到这一点,你必须有一个方案来管理发生在带外的pin码,这意味着通过不同的渠道。当然,对于谷歌api这样的服务来说,保护密钥是不实际的。

最安全的解决方案是将密钥保存在服务器上,并通过服务器路由所有需要该密钥的请求。这样,密钥就不会离开服务器,所以只要服务器是安全的,那么密钥也是安全的。当然,这种解决方案有性能成本。