在很多的Android项目中都需要用户登录、注册。这样的话在开发中做好保护用户密码的工作就显得尤为重要。这里我把自己的密码保护方法记录下来。
这是我建了一个保存密码的文件,以便于检查自己保存密码或者上传到服务器的时候密码是否已经被保护了。这就是当我输入用户名和密码之后点击记住密码之后
保存在SD卡上的文件,打开之后可以明显的看到密码已经被保护了。
下面是我的布局文件以及主程序的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | package com.itcast.test03; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NamevaluePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNamevaluePair; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements onClickListener { private EditText et_username; private EditText et_userPsd; private Button login; private Button signUp; private CheckBox save; private String user,pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_username = (EditText)findViewById(R.id.et_number); et_userPsd = (EditText)findViewById(R.id.et_password); login=(Button)findViewById(R.id.login); signUp=(Button)findViewById(R.id.signUp); save = (CheckBox)findViewById(R.id.save); save.setonClickListener( new CheckBox.onClickListener(){ public void onClick(View v) { SharedPreferences pre = getSharedPreferences( "loginvalue" , MODE_WORLD_WRITEABLE); pass = MD5( et_userPsd. getText ().toString()); user = et_username. getText ().toString(); if (!pass.equals( "" ) && !user.equals( "" )) { pre.edit().putString( "username" , et_username. getText ().toString()) .putString( "password" , encryptmd5(pass)).commit(); Toast.makeText(getApplicationContext(), "保存成功!" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "密码不能为空!" , Toast.LENGTH_LONG).show(); } } }); login.setonClickListener( new Button.onClickListener() { @Override public void onClick(View v) { SharedPreferences sp = getSharedPreferences( "loginvalue" ,MODE_WORLD_READABLE); String loginuser = sp.getString( "username" ,null); String loginpass = sp.getString( "password" ,null); user = et_username. getText ().toString(); pass = et_userPsd. getText ().toString(); String passmd5 = MD5(pass); String encryptmd5 = encryptmd5(passmd5); System.out.println( "username=" + loginuser + "-------------password=" + loginpass); System.out.println( "user==" + user + "-------------encryptmd5==" + encryptmd5); if (!user.equals( "" ) && !pass.equals( "" )) { if (user.equals(loginuser) && encryptmd5.equals(loginpass)) { Intent intent = new Intent(); intent.setClass(MainActivity.this, StudentMainActivity. class ); MainActivity.this.startActivity(intent); finish(); } else { Toast.makeText(getApplicationContext(), "密码是错误的!" , Toast.LENGTH_LONG).show(); } } else { Toast.makeText(getApplicationContext(), "密码不能为空!" , Toast.LENGTH_LONG).show(); } } }); initWidget(); // } private void initWidget() { login.setonClickListener(this); signUp.setonClickListener(this); et_username.setonFocusChangeListener( new onFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if (!hasFocus){ String username=et_username. getText ().toString().trim(); if (username.length()<4){ Toast.makeText(MainActivity.this, "用户名不能小于4个字符" , Toast.LENGTH_SHORT); } } } }); et_userPsd.setonFocusChangeListener( new onFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if (!hasFocus){ String password=et_userPsd. getText ().toString().trim(); if (password.length()<4){ Toast.makeText(MainActivity.this, "密码不能小于4个字符" , Toast.LENGTH_SHORT); } } } }); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.login: if (checkEdit()) { login(); } break ; case R.id.signUp: Intent intent2= new Intent(MainActivity.this,SignUp. class ); startActivity(intent2); break ; } } private boolean checkEdit(){ if (et_username. getText ().toString().trim().equals( "" )){ Toast.makeText(MainActivity.this, "用户名不能为空" , Toast.LENGTH_SHORT).show(); Intent intent= new Intent(MainActivity.this,StudentMainActivity. class ); startActivity(intent); } else if (et_userPsd. getText ().toString().trim().equals( "" )){ Toast.makeText(MainActivity.this, "密码不能为空" , Toast.LENGTH_SHORT).show(); } else { return true; } return false; } private void login(){ //这个网址需要改动 String httpUrl= "http://192.168.1.102:8080/web-test/login.jsp" ; HttpPost httpRequest= new HttpPost(httpUrl); List<namevaluepair> params= new ArrayList<namevaluepair>(); params.add( new BasicNamevaluePair( "username" ,et_username. getText ().toString().trim())); params.add( new BasicNamevaluePair( "password" ,et_userPsd. getText ().toString().trim())); HttpEntity httpentity = null; try { httpentity = new UrlEncodedFormEntity(params, "utf8" ); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } httpRequest.setEntity(httpentity); HttpClient httpclient= new DefaultHttpClient(); HttpResponse httpResponse = null; try { httpResponse = httpclient.execute(httpRequest); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (httpResponse.getStatusLine().getStatusCode()==200) { String strResult = null; try { strResult = EntityUtils.toString(httpResponse.getEntity()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(MainActivity.this, strResult, Toast.LENGTH_SHORT).show(); Intent intent= new Intent(MainActivity.this,StudentMainActivity. class ); startActivity(intent); } else { Toast.makeText(MainActivity.this, "登录失败!" , Toast.LENGTH_SHORT).show(); } } public static String MD5(String str){ MessageDigest md5 = null; try { md5 = MessageDigest.getInstance( "MD5" ); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); return "" ; } char[] charArray = str.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) { byteArray[i] = (byte)charArray[i]; } byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int)md5Bytes[i])&0xff; if (val<16){ hexValue.append( "0" ); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } public static String encryptmd5(String str){ char[] a = str.toCharArray(); for (int i = 0; i < a.length; i++) { a[i] = (char)(a[i]^ '1' ); } String s = new String(a); return s; } } </namevaluepair></namevaluepair> |
添加权限:
1 2 | <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" > <uses-permission android:name= "android.permission.INTERNET" ></uses-permission></uses-permission> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。