如何在 Android 应用程序中隐藏 API 键?
How to hide API keys in Android application?
在我的应用程序中,我使用 google 映射 api 键与 google 进行通信。我在 gradle debug/release 配置中定义了这个 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 令牌。这是真的?如果是,在我的代码中隐藏 api 键的方法是什么?
简答:
根据 Google Map Documentation 混淆或加密您的 google 映射 API 密钥:
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++ 中的密钥作为库进行 base64 编码,只需调用 [=39 中的函数即可使用它=] class,因为编译后的 C++ 代码比你的 Java classes 更难反编译。
长答案:视情况而定
你不能在客户端做任何事情来证明你的代码不被反编译,特别是让一个硬编码字符串免疫被提取。
那你应该怎么办?
这是一个权衡,首先你应该看看你的密钥对你有多重要,谁会尝试访问它以及他花多少时间来破解你的应用程序。通过加密或混淆,我们只会让它变得更难,所以我们需要更多的专业人士和更多的时间来破解它(基本上我们添加的每一层安全性都在这样做。)
如果它太机密并且如果它泄漏会给你带来很多问题并且你应该将它存储在服务器中是至关重要的,那么你应该向该服务器请求并且该服务器将执行 API 调用您的密钥或使用 JWT 令牌之类的东西。
但是在Google映射API的情况下只要你监控它并正确配置它,泄漏你的密钥不会造成很多问题你.
在我的应用程序中,我使用 google 映射 api 键与 google 进行通信。我在 gradle debug/release 配置中定义了这个 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 令牌。这是真的?如果是,在我的代码中隐藏 api 键的方法是什么?
简答:
根据 Google Map Documentation 混淆或加密您的 google 映射 API 密钥:
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++ 中的密钥作为库进行 base64 编码,只需调用 [=39 中的函数即可使用它=] class,因为编译后的 C++ 代码比你的 Java classes 更难反编译。
长答案:视情况而定
你不能在客户端做任何事情来证明你的代码不被反编译,特别是让一个硬编码字符串免疫被提取。
那你应该怎么办?
这是一个权衡,首先你应该看看你的密钥对你有多重要,谁会尝试访问它以及他花多少时间来破解你的应用程序。通过加密或混淆,我们只会让它变得更难,所以我们需要更多的专业人士和更多的时间来破解它(基本上我们添加的每一层安全性都在这样做。)
如果它太机密并且如果它泄漏会给你带来很多问题并且你应该将它存储在服务器中是至关重要的,那么你应该向该服务器请求并且该服务器将执行 API 调用您的密钥或使用 JWT 令牌之类的东西。
但是在Google映射API的情况下只要你监控它并正确配置它,泄漏你的密钥不会造成很多问题你.