使用哈希用户名和密码的Android登录
问题描述:
我正在开发一个项目,用户必须首先在应用程序中登录才能使用它。而我的小问题是,当用户输入他的用户名和密码时,我必须将它们散列并发送到服务器,而不是用户输入的用户名和密码。所以现在我在做这样的事情:使用哈希用户名和密码的Android登录
EditText txtUserName = (EditText) findViewById (R.id.username_login_input);
EditText txtPassword = (EditText) findViewById (R.id.password_login_input);
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> postParameters;
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.rpc.shalqlqlq.com");
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username_hash", hashUser(txtUserName.getText().toString(),txtPassword.getText().toString())));
postParameters.add(new BasicNameValuePair("password_hash", hashPass(txtUserName.getText().toString(),txtPassword.getText().toString())));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);
Log.w("Response ","Status line : "+ response.getStatusLine().toString());
byte[] buffer = new byte[1024];
buffer = EntityUtils.toString(response.getEntity()).getBytes();
public String hashUser(String username, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
String hashUser = SHA1.Sha1Hash(username);
String hashPass = SHA1.Sha1Hash(password);
String luser = hashPass+hashUser;
String lastUser = SHA1.Sha1Hash(luser);
return lastUser;
}
public String hashPass(String username, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
String hashUser = SHA1.Sha1Hash(username);
String hashPass = SHA1.Sha1Hash(password);
String lpass = hashPass+hashUser;
String lastPass = SHA1.Sha1Hash(lpass);
return lastPass;
}
它仍然告诉我,用户名和密码不正确。我很确定哈希是正确的,因为我已经尝试过了。那么任何人都可以帮助我找出我的错误在哪里?
答
如果一切正常,我想也许这个问题是从edittext
获取用户名和密码。尝试把
postParameters.add(new BasicNameValuePair("username_hash", hashUser(txtUserName.getText().toString(),txtPassword.getText().toString())));
postParameters.add(new BasicNameValuePair("password_hash", hashPass(txtUserName.getText().toString(),txtPassword.getText().toString())));
在一些一种事件。我想你有一个按钮登录..所以你设置这些参数onClick
方法,我认为它会工作。
答
一开始,你的哈希的用户名和散列密码是相同的:
public String hashUser(String username, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
String hashUser = SHA1.Sha1Hash(username);
String hashPass = SHA1.Sha1Hash(password);
String luser = hashPass+hashUser; // <-- Hashed pass + user
String lastUser = SHA1.Sha1Hash(luser); // <-- Hashed a second time
return lastUser;
}
public String hashPass(String username, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
String hashUser = SHA1.Sha1Hash(username);
String hashPass = SHA1.Sha1Hash(password);
String lpass = hashPass+hashUser; // <-- Hashed pass + user
String lastPass = SHA1.Sha1Hash(lpass); // <-- Hashed a second time
return lastPass;
}
除非服务器存储用户名和密码作为每一个用户名和密码的哈希散列,然后我猜这是你的问题是。
为什么不直接使用这样的:
postParameters.add(new BasicNameValuePair("username_hash", SHA1.Sha1Hash(txtUserName.getText().toString())));
postParameters.add(new BasicNameValuePair("password_hash", SHA1.Sha1Hash(txtPassword.getText().toString())));
+0
我的错误,用户名和密码不一样,我在这里复制粘贴代码时犯了一个错误。 –
它的工作原理!非常感谢您的帮助! –