在之前的文章中,我们将用户的密码使用SharedPreferences存储,我们打开/data/data/com.wuyudong.mobilesafe/shared_prefs文件夹下的 config.xml 文件,导入到本地,查看内容:
XML/HTML代码
- <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
- <map>
- <string name="mobile_safe_psd">123</string>
- <boolean name="open_update" value="false" />
- </map>
密码居然使用的是明文,这样是非常不安全的。这里采用md5加密
编写Md5Util工具类,代码如下:
Java代码
- package com.wuyudong.mobilesafe.Utils;
-
-
-
-
-
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
-
- public class Md5Util {
-
-
-
-
-
-
- public static String encoder(String psd) {
- try {
-
- psd = psd + "mobilesafe";
-
- MessageDigest digest = MessageDigest.getInstance("MD5");
-
- byte[] bs = digest.digest(psd.getBytes());
-
-
- StringBuffer stringBuffer = new StringBuffer();
- for (byte b : bs) {
- int i = b & 0xff;
-
- String hexString = Integer.toHexString(i);
- if (hexString.length() < 2) {
- hexString = "0" + hexString;
- }
- stringBuffer.append(hexString);
- }
-
- System.out.println(stringBuffer.toString());
- return stringBuffer.toString();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- return "";
- }
- }
md5加密:将字符串转换成 32位的字符串(16进制字符(0~f)) 不可逆
例如:123加密后:202cb962ac59075b964b07152d234b70
接下来直接调用加密类即可,分别在“设置密码”和“确认密码”两个对话框进行加密比对,具体代码如下:
Java代码
-
-
-
- private void showConfirmPsdDialog() {
-
- Builder builder = new Builder(this);
- final AlertDialog dialog = builder.create();
- final View view = inflate(this, R.layout.dialog_confirm_psd, null);
-
- dialog.setView(view);
- dialog.show();
-
- Button bt_submit = (Button) view.findViewById(R.id.bt_submit);
- Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);
-
- bt_submit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- EditText et_confirm_psd = (EditText) view.findViewById(R.id.et_confirm_psd);
- String confirmPsd = et_confirm_psd.getText().toString();
-
- if (!TextUtils.isEmpty(confirmPsd)) {
-
- String psd = SpUtil.getString(getApplicationContext(), ConstantValue.MOBILE_SAFE_PSD, "");
-
- if (psd.equals(Md5Util.encoder(confirmPsd))) {
-
- Intent intent = new Intent(getApplicationContext(), testActivity.class);
- startActivity(intent);
-
- dialog.dismiss();
- } else {
- ToastUtil.show(getApplicationContext(), "输入密码错误");
- }
-
- } else {
-
- ToastUtil.show(getApplicationContext(), "请输入密码");
- }
- }
- });
- bt_cancel.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- dialog.dismiss();
- }
- });
- }
-
-
-
-
- private void showSetPsdDialog() {
-
- Builder builder = new Builder(this);
- final AlertDialog dialog = builder.create();
- final View view = inflate(this, R.layout.dialog_set_psd, null);
-
- dialog.setView(view);
- dialog.show();
-
- Button bt_submit = (Button) view.findViewById(R.id.bt_submit);
- Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);
-
- bt_submit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- EditText et_set_psd = (EditText) view.findViewById(R.id.et_set_psd);
- EditText et_confirm_psd = (EditText) view.findViewById(R.id.et_confirm_psd);
- String psd = et_set_psd.getText().toString();
- String confirmPsd = et_confirm_psd.getText().toString();
- if (!TextUtils.isEmpty(psd) && !TextUtils.isEmpty(confirmPsd)) {
-
- if (psd.equals(confirmPsd)) {
- Intent intent = new Intent(getApplicationContext(), testActivity.class);
- startActivity(intent);
-
- dialog.dismiss();
- SpUtil.putString(getApplicationContext(), ConstantValue.MOBILE_SAFE_PSD, Md5Util.encoder(psd));
-
- } else {
- ToastUtil.show(getApplicationContext(), "密码不一致");
- }
-
- } else {
-
- ToastUtil.show(getApplicationContext(), "请输入密码");
- }
- }
- });
- bt_cancel.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- dialog.dismiss();
- }
- });
- }