您的位置:首页 > 其它

手机安全卫士------手机防盗页面之自定义对话框&MD5加密

2015-07-24 04:20 477 查看
功能需求:

用户点击主页面上的“手机防盗”按钮时,判断用户是否设置过密码。

如果没有设置过,则弹出输入密码对话框

如果设置过了,则弹出设置密码对话框

用户的密码要进行MD5加密之后再存储在内存中

技术点:

- 自定义对话框的使用

- MD5加密的实现方式

- SharedPreferences的读写操作

自定义对话框

1.在layout目录下创建一个布局文件,把自定义的对话框布局设置成功

具体代码实现如下

设置密码对话框的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_dialogsetuppwd_title"
android:text="设置密码"
android:textSize="36sp"
android:textColor="#000"
android:background="#66ff6600"
android:gravity="center_horizontal"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:id="@+id/et_dialogsetuppwd_pwd"
android:textSize="30sp"
android:textColor="#000"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入确认密码"
android:inputType="textPassword"
android:id="@+id/et_dialogsetuppwd_checkpwd"
android:textSize="30sp"
android:textColor="#000"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_dialogsetuppwd_ok"
android:text="确定"
android:layout_marginRight="10dp"
android:layout_marginLeft="20dp"
android:textSize="28sp"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_dialogsetuppwd_cancel"
android:textSize="28sp"
android:text="取消"
android:layout_marginRight="20dp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</LinearLayout>


还有输入密码对话框的布局文件,不再赘述。。

2.加载自定义布局到对话框的操作:

AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
View view = View.inflate(HomeActivity.this,R.layout.dialog_input_password,null);
builder.setView(view);
mEtPwd = (EditText) view.findViewById(R.id.et_dialoginputpwd_pwd);
mBtnOk = (Button) view.findViewById(R.id.btn_dialoginputpwd_ok);
mBtnCancel = (Button) view.findViewById(R.id.btn_dialoginputpwd_cancel);
mBtnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
String password = mEtPwd.getText().toString().trim();
if(TextUtils.isEmpty(password))
{
Toast.makeText(HomeActivity.this,"密码不能为空,请重新输入",Toast.LENGTH_SHORT).show();
}
else
{
String checkPassword = sp.getString("password",null);
if(password.equals(checkPassword))
{
//进入主页面
dialog.dismiss();
}
else
{
Toast.makeText(HomeActivity.this,"密码错误,请重新输入",Toast.LENGTH_SHORT).show();
}
}
}
});
mBtnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog = builder.show();
}


逻辑:

判断password的值是否为空,如果为空,则 弹出设置密码对话框;否则,弹出输入密码对话框。

如果弹出的是设置密码对话框,当两次输入的密码相同且不为空的时候,存储密码到SharedPreferences.

如果弹出的是输入密码对话框,当用户输入的密码和SharedPreferences中存储的密码相同时,跳转到下一个页面,否则,吐司报错

MD5加密

MD5加密是不可逆的,即加密后的密文无法转换回原来的内容

MD5加密实现的思路:

获得一个信息摘要器MessageDigest

把信息摘要器实例化,并确定采用MD5的加密方式

把字符串转换成字节数组

把字节数组通过digest()方法转换成MD5加密后的字节数组

把数组遍历

对字节数组中的每一个字节进行与运算(加盐),返回int类型

调用Integer.toHexString(int) 把int类型转换成十六进制字符串

如果字符串的长度为1,则进行补零操作

添加字符串到StringBuffer中

StringBuffer调用toString()转换成字符串,即为结果。

代码示例:

//把数据进行MD5加密
public String getTextByMD5(String text)
{
StringBuffer buffer = new StringBuffer();
try
{
//1.创建一个信息摘要器
MessageDigest digest = MessageDigest.getInstance("md5");
//2.调用digest方法,把数据进行MD5第一次加密
byte[] bytes = digest.digest(text.getBytes());
//3.遍历数组
for(byte b : bytes)
{
//4.进行与运算
int number = b & 0xff;
//5.把运算结果转换成十六进制字符串
String hex = Integer.toHexString(number);
//6.添加字符串到buffer
if(hex.length() == 1)
{
buffer.append("0");
}
buffer.append(hex);
}
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return buffer.toString();
}


不知不觉天亮了~~睡意袭来,五个小时之后继续战斗!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息