博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 手机卫士--md5加密过程
阅读量:4660 次
发布时间:2019-06-09

本文共 5447 字,大约阅读时间需要 18 分钟。

在之前的文章中,我们将用户的密码使用SharedPreferences存储,我们打开/data/data/com.wuyudong.mobilesafe/shared_prefs文件夹下的 config.xml 文件,导入到本地,查看内容:

123

密码居然使用的是明文,这样是非常不安全的。这里采用md5加密

本文地址:,转载请注明出处。

编写Md5Util工具类,代码如下:

package com.wuyudong.mobilesafe.Utils;/** * Created by wuyudong on 2016/10/9. */import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class Md5Util {    /**     * 给指定字符串按照md5算法去加密     *     * @param psd 需要加密的密码    加盐处理     * @return md5后的字符串     */    public static String encoder(String psd) {        try {            //加盐处理            psd = psd + "mobilesafe";            //1,指定加密算法类型            MessageDigest digest = MessageDigest.getInstance("MD5");            //2,将需要加密的字符串中转换成byte类型的数组,然后进行随机哈希过程            byte[] bs = digest.digest(psd.getBytes());       //3,循环遍历bs,然后让其生成32位字符串,固定写法            //4,拼接字符串过程            StringBuffer stringBuffer = new StringBuffer();            for (byte b : bs) {                int i = b & 0xff;                //int类型的i需要转换成16机制字符                String hexString = Integer.toHexString(i);                if (hexString.length() < 2) {                    hexString = "0" + hexString;                }                stringBuffer.append(hexString);            }            //5,打印测试            System.out.println(stringBuffer.toString());            return stringBuffer.toString();        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        }        return "";    }}

md5加密:将字符串转换成 32位的字符串(16进制字符(0~f)) 不可逆

例如:123加密后:202cb962ac59075b964b07152d234b70

接下来直接调用加密类即可,分别在“设置密码”和“确认密码”两个对话框进行加密比对,具体代码如下:

/**     * 确认密码对话框     */    private void showConfirmPsdDialog() {        //需要自己去定义对话框的显示样式,所以要调用dialog.setView(view);        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)) {                    //将存储在sp中32位的密码,获取出来,然后将输入的密码同样进行md5,然后与sp中存储密码比对                    String psd = SpUtil.getString(getApplicationContext(), ConstantValue.MOBILE_SAFE_PSD, "");                    if (psd.equals(Md5Util.encoder(confirmPsd))) {                        //进入用户手机防盗模块,开启一个新的activity                        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() {        //需要自己去定义对话框的显示样式,所以要调用dialog.setView(view);        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();            }        });    }

 

转载于:https://www.cnblogs.com/wuyudong/p/5941131.html

你可能感兴趣的文章
IOS Xcode -> instruments -> Leaks
查看>>
工作中常用的Linux命令:crontab命令,定时任务执行命令
查看>>
【转载】C#中List集合使用Remove方法移除指定的对象
查看>>
Android Studio 第一次配置及其使用
查看>>
Little Girl and Maximum Sum CodeForces - 276C
查看>>
expect 交互 之shell执行命令操作
查看>>
java1.8新特性(三 关于 ::的用法)
查看>>
《python机器学习—预测分析核心算法》:理解数据
查看>>
xps文档打印后winform界面文字丢失
查看>>
不同的领导,不同的关注点
查看>>
虚拟机报错:无法打开内核设备"\\.\Global\vmx86":
查看>>
Chapter 3 Phenomenon——8
查看>>
网络编程
查看>>
sublime text 按下Ctrl + B 显示空白的解决办法
查看>>
.net批量上傳Csv檔資料應用程序開發總結
查看>>
Java知识点总结
查看>>
C#中的String.Format()方法
查看>>
hdu 1000&hdu1001
查看>>
堆积木----vector防止内存超限
查看>>
MySQL 5.5 表分区功能增强
查看>>