在我的应用程序中,我使用 google maps api key 与 google 进行通信。我在 gradle 调试/发布配置中这样定义了这个 MAP_KEY:
debug {
buildConfigField 'String', 'BASE_URL', '"https://myweb.com"'
buildConfigField 'String', 'SOCKET_URL', '"https://socket.myweb.com"'
buildConfigField 'String', 'MAP_KEY', '"azaS****Ds"'
}
但是如果有人从 google play 商店下载我的应用程序并反编译它,他将获得我的 API token 。这是真的?如果是,在我的代码中隐藏 api key 的方法是什么?
简答:
根据 Google Map Documentation 混淆或加密您的谷歌地图 API key :
On mobile apps that use Maps Web Service APIs, consider one or more of the following techniques to further safeguard your apps and API
keys:
Apply an API restriction on the API key. This action narrows the scope of the API key to the APIs you associate with the key.
Obfuscate or encrypt the API key. This action complicates key
scraping attempts directly from the application.
Use CA pinning or certificate pinning to verify the server resources are valid. CA pinning checks that a server's certificate
was issued by a trusted certificate authority, and prevents
Man-In-The-Middle attacks that could lead to a third party
discovering your API key. Certificate pinning goes further by
extracting and checking the public key included in the server
certificate. Pinning is useful for mobile clients communicating
directly with Google servers, as well as mobile clients
communicating with the developer's own proxy server.
Use a proxy server.
您可以使用许多只需快速搜索即可轻松找到的加密或混淆方法,一种常见的方法是将 C++ 中的 key 作为库进行 base64 编码,并且只需调用 Java 类中的函数即可使用它,因为 C++编译后的代码比 Java 类更难反编译。
Tutorial of doing it with NDK
长答案:视情况而定
你不能在客户端做任何事情来证明你的代码不被反编译,特别是让一个硬编码字符串免疫被提取。
那么你应该做什么?
这是一个权衡,首先你应该看看你的 key 对你有多重要,谁会尝试访问它,以及他花多少时间来破解你的应用程序。通过加密或混淆,我们只会让它变得更难,所以我们需要更多的专业人士和更多的时间来破解它(基本上我们添加的每一层安全性都在这样做。)
如果它太 secret 并且如果它泄漏会给你带来很多问题并且你应该将它存储在服务器中是至关重要的,那么你应该向该服务器请求并且该服务器将使用你的 key 或类似的东西进行 API 调用一个 JWT token 。
但是如果是 Google Map API,只要您对其进行监控并正确配置,泄漏您的 key 不会给您带来很多问题。