Android 手機衛士--md5加密過程

來源:互聯網
上載者:User

標籤:

在之前的文章中,我們將使用者的密碼使用SharedPreferences儲存,我們開啟/data/data/com.wuyudong.mobilesafe/shared_prefs檔案夾下的 config.xml 檔案,匯入到本地,查看內容:

<?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加密

本文地址:http://www.cnblogs.com/wuyudong/p/5941131.html,轉載請註明出處。

編寫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

Android 手機衛士--md5加密過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.