简体   繁体   English

Android –安全地向服务器发送数据/从服务器接收数据

[英]Android – Sending/Receiving data to/from server securely

I am developing an Android app. 我正在开发一个Android应用程序。 The app communicates with a server through a PHP API. 该应用程序通过PHP API与服务器通信。 Each user must create an account. 每个用户必须创建一个帐户。 So, the app has a login functionality. 因此,该应用程序具有登录功能。 I am doing further research on how to be able to securely transfer data between client (Android app) and server. 我正在进一步研究如何在客户端(Android应用程序)和服务器之间安全地传输数据。 For example, a user sends, through a POST, request his username/password in order to login. 例如,用户通过POST发送请求其用户名/密码以便登录。

Based on what I have read, I can safely assume that in case someone “listens” the transaction between client and server he could steal the username/password combination and use it on order to login to the legitimate user's account. 根据我所阅读的内容,我可以安全地假设,如果有人“侦听”客户端和服务器之间的交易,他可以窃取用户名/密码组合并使用它来登录合法用户的帐户。 Is that correct? 那是对的吗?

The solution to this problem is to encrypt the data (eg username and password) before sending them either from client or server. 解决此问题的方法是先加密数据(例如,用户名和密码),然后再从客户端或服务器发送数据。 The data will be then decrypted by the recipient (client or server). 然后,数据将被接收方(客户端或服务器)解密。 I do that by using crypt/decrypt functions both on client (written in Java) and server (written in PHP). 我通过在客户端(用Java编写)和服务器(用PHP编写)上使用crypt / decrypt函数来做到这一点。 Each function has the same IV (initialization vector) and Secret Key (to be honest, I do not know much about the IV's usage so forgive me if I say something wrong. I google around for information but any useful links would be really appreciated). 每个函数都具有相同的IV(初始化向量)和秘密密钥(说实话,我对IV的用法了解不多,因此如果我说错了请原谅。我在Google周围搜集信息,但任何有用的链接将不胜感激) 。

From what I read, the problem with this implementation is that the APK file could be decompiled from client side and get the IV and Secret Key. 据我了解,此实现的问题在于,APK文件可能会从客户端反编译并获得IV和密钥。 As a result, a listener could decrypt the data sent. 结果,侦听器可以解密发送的数据。 Is that correct? 那是对的吗?

Trying to find a solution to this problem I have a suggestion and I would like your opinion. 试图找到解决这个问题的方法,我有一个建议,我希望您的意见。 What if during user's registration a unique IV and secret key are given to the each user. 如果在用户注册期间为每个用户提供了唯一的IV和密钥,该怎么办。 These values are stored both to a MySQL database (server side) and a SQLite database (client side). 这些值既存储到MySQL数据库(服务器端),又存储到SQLite数据库(客户端)。 Whenever data needs to be sent trough a post request, the user's id (could be something simple as an integer) and the data to be sent are encrypted using the unique IV/Secret Key for the individual user. 每当需要通过发布请求发送数据时,用户的ID(可以是简单的整数)和要发送的数据都将使用针对每个用户的唯一IV /秘密密钥进行加密。 These are stored locally so the “listener” has no access to them. 它们存储在本地,因此“侦听器”无法访问它们。 Even if he decompile the APK he will just have access to his own IV/Secret Key that he already knows. 即使他反编译APK,他也将只能访问他已经知道的自己的IV /秘密密钥。 Then on server side the data are decrypted using the same IV/Secret Key stored on the server. 然后在服务器端,使用存储在服务器上的相同IV /秘密密钥解密数据。 The same procedure is applied when data are sent from server to client. 从服务器向客户端发送数据时,将应用相同的过程。

Is this a correct approach? 这是正确的方法吗?

Reusing the same symmetric key and same IV is extremely incorrect approach and must not be used ever. 重用相同的对称密钥和相同的IV是极其不正确的方法,并且永远不能使用。

Reusing the same key and IV will enable attacks where the attacker will be able to recover your secret key just be eavesdropping on the encrypted traffic for long enough. 重用相同的密钥和IV将启用攻击,攻击者只要窃听加密流量足够长的时间,便能够恢复您的秘密密钥。 And when the attacker has your key he will be able to decrypt all and every past and future communications. 当攻击者获得您的密钥时,他将能够解密所有过去和将来的通信。

To secure the data transfer you should use HTTPS (or SSL/TLS directly if your data transfer protocol is not HTTP-based). 为了保护数据传输,您应该使用HTTPS(如果您的数据传输协议不是基于HTTP的,则直接使用SSL / TLS)。

If your only concern is to securely communicate with the server i suggest you to install a ssl certificate to your server. 如果您唯一关心的是与服务器安全通信,建议您将ssl证书安装到服务器。 Doing this way the communication will be secured by the underlying protocol. 这样做,将通过基础协议保护通信。 To facilitate your communication with the server for implementing ssl communication i suggest you to use aquery library, here's a link !. 为了促进与服务器的通信以实现ssl通信,我建议您使用查询库,这里有一个链接 !。 Also dont forget to see the ca compatibility list for android. 也不要忘记查看Android的ca兼容性列表。

Hope it helps. 希望能帮助到你。

This is a rather old question so i'm surprised it didn't have a more complete response. 这是一个相当老的问题,所以我很惊讶它没有更完整的答复。 It seems you understand the concepts of symmetric encryption but you are missing knowledge of public/private key encryption. 似乎您了解对称加密的概念,但是缺少公共/专用密钥加密的知识。 Look up RSA for a method of achieving public/private key encryption. RSA中查找实现公钥/私钥加密的方法。 With this, you can generate (via the assistance of a cryptographically secure RNG) new random keys as well as IVs at the start of each session to feed to your symmetric encryption system. 这样,您可以在每个会话开始时(通过加密安全的RNG协助)生成新的随机密钥以及IV,以馈入对称加密系统。 This means that anyone listening from start to finish will not be able to make sense of your system short of brute forcing the RSA key or the symetric(AES?) key. 这意味着,从头到尾收听的任何人都将无法通过强行使用RSA密钥或symetric(AES?)密钥来理解您的系统。

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

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