Android登录时密码保护功能
作者:kang_ya_ping 发布时间:2023-10-24 07:09:40
标签:Android,登录,密码
在很多的Android项目中都需要用户登录、注册。这样的话在开发中做好保护用户密码的工作就显得尤为重要。这里我把自己的密码保护方法记录下来。
这是我建了一个保存密码的文件,以便于检查自己保存密码或者上传到服务器的时候密码是否已经被保护了。这就是当我输入用户名和密码之后点击记住密码之后
保存在SD卡上的文件,打开之后可以明显的看到密码已经被保护了。
下面是我的布局文件以及主程序的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_head"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_head"
android:layout_margin="10dp"
android:background="#FFFFFF"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="账号"/>
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_name"
android:background="@null"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E6E6E6"/>
<RelativeLayout
android:id="@+id/rl_userpsd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/tv_psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="密码"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_psw"
android:inputType="textPassword"
android:background="@null"/>
</RelativeLayout>
</LinearLayout>
<Button
android:id="@+id/login"
android:onClick="login"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/layout"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#3C8DC4"
android:text="登录"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/signUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:background="#E6E6E6"
android:textColor="#000000"
android:layout_marginTop="21dp"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:layout_below="@+id/login"
android:layout_alignParentRight="true"/>
<CheckBox
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/signUp"
android:layout_alignBottom="@+id/signUp"
android:layout_alignLeft="@+id/login"
android:layout_marginLeft="14dp"
android:text="记住密码" />
</RelativeLayout>
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;
}
}
添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
0
投稿
猜你喜欢
- C# 泛型(Generic)定义:泛型允许我们延迟编写类或方法中的编程元素的数据类型的规范,直到实际在程序中使用它的时候。也就是说,泛型是可
- public bool isUtf8(byte[] rawText)
- 简单几步,实现SpringMVC+servlet3.0文件上传功能:第一步:配置web.xml文件中的servlet,添加multipart
- 本文实例为大家分享了C#生成唯一订单号的具体代码,供大家参考,具体内容如下根据GUID+DateTime.Now.Ticks生产唯一订单号/
- springMVC的生命周期,听到的时候都没有反应过来,springMVC还有生命周期?现在看来生命周期就是springMVC的流程,Spr
- 本文实例讲述了Java实现数据库连接池的方法。分享给大家供大家参考。具体如下:package com.kyo.connection;impo
- 一、根据流向分为输入流和输出流:注意输入流和输出流是相对于程序而言的。输出:把程序(内存)中的内容输出到磁盘、光盘等存储设备中输入:读取外部
- 投篮小游戏规则,点击投篮目标点,就会有一个球沿着相关抛物线,然后,判断是否进入篮子里,其实就是一个矩形,直接是按照碰撞检测来的,碰到就算进去
- 快速排序过程没有既不浪费空间又可以快一点的排序算法呢?那就是“快速排序”!光听这个名字是不是就觉得很高端呢。假设我们现在对“52 39 67
- 前言本文主要给大家介绍的是java虚拟机的故障处理工具,文中提到这些工具包括:名称主要作用jpsJVM process Status Too
- 本文实例讲述了Android开发实现查询远程服务器的工具类QueryUtils。分享给大家供大家参考,具体如下:/** * 查询远程服务器的
- 定义:一个对象应该对其他对象了解最少迪米特法则的核心观念就是类间解耦,弱耦合,只有弱耦合了以后,类的复用性才可以提高。形象一点的比喻类似于:
- java常量池是一个经久不衰的话题,也是面试官的最爱,题目花样百出,这次好好总结一下。理论先拙劣的表达一下jvm虚拟内存分布:程序计数器是j
- 一、概述顶部ViewPager指示器的字体变色,该效果图是这样的:大概是今天头条的app,神奇的地方就在于,切换ViewPager页面的时候
- Spring流程Debug1.1 Spring测试环境搭建Spring模块概览,绿色是模块,Spring中八大模块,黑色表示该模块包含的ja
- 前言列表是移动应用中用得最多的组件了,我们也会经常对列表元素进行增加或删除操作,最简单的方法是列表数据变动后,直接 setStat
- 前言最近在项目中使用到定时任务,之前一直都是使用Quartz 来实现,最近看Spring 基础发现其实Spring 提供 Spring Sc
- 对于使用avalonia的时候某些功能需要到一些提示,比如异常或者成功都需要对用户进行提示,所以需要单独实现弹窗功能,并且可以自定义内部组件
- 一直写过数组全排列的算法,当时接触的是使用回溯的方法,这样可以保证生成的全排列一定是按照字典序的,但是今天在做leetcode上的一道题时,
- 问题:对于多线程编程,很多时候往往需要向线程中传递多个参数,而C#中的线程只接收1个object类型的参数(如下):Thread t = n