我正在开发一个 android 应用程序,我在其中向服务器发送一些文本和文件。我正在使用 JSON 将文件上传到服务器。但我想提供上传数据的安全性。数据只能从安卓设备上传。在我的例子中,如果我使用 postman 发送数据,它会上传到服务器上。
php 开发人员告诉我,“在 header 中发送数据”。我用谷歌搜索,找到了一些代码。
Authentication.java
public class Authentication {
public void AuthenticateData(String url) {
HttpPost httppost = new HttpPost(url);
httppost.addHeader("userId","someName");
httppost.addHeader("secretKey","password");
}
}
我就是这样用的。
Authentication authentication
URL = "url_to_upload_data";
JSONParser jParser = new JSONParser();
.
.
authentication.AuthenticateData(URL);
.
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("emailid", emailid));
param.add(new BasicNameValuePair("device_reg", deviceRegNo));
param.add(new BasicNameValuePair("message",message));"));
JSONObject json_Object = jParser.makeHttpRequest(URL, "POST", param);
这是正确的做法吗?它会安全吗?
最佳答案
您的代码存在的问题是 AuthentificateData
没有返回任何内容。
您的方法的问题是您假设密码可以安全地存储在手机上。如果你把它放在代码中,即使在混淆之后任何人都可以反编译你的源代码并读取它的值。
为了提供安全传输,您需要使用非对称加密,也称为 public key cryptography .公钥可以为任何人所知,并且可以安全地存储在手机中。您必须保护您的服务器,以免泄露您的私钥。
为了帮助进行身份验证,您还可以使用 digital signatures.
您不必从头开始实现它们。你在这里simple examples开始。
关于java - 在 android 中的服务器上上传之前验证数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35500497/