Android开发库VUtils之MD5加密

来源:互联网 发布:近年淘宝发展历程概述 编辑:程序博客网 时间:2024/05/22 03:47

Android开发中,在处理密码的时候经常用到MD5加密,那么我们就写个将密码通过MD5加密,再转成16进制的类吧!

package com.v.vutils.utils;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Utils {    protected static char[] sHexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',            'f' };    public static String getMD5String(String str) {        if (str == null)            return null;        return getMD5String(str.getBytes());    }    public static String getMD5String(byte[] bytes) {        try {            MessageDigest messageDigest = MessageDigest.getInstance("MD5");            bytes = messageDigest.digest(bytes);            StringBuffer stringbuffer = new StringBuffer();            for (int i = 0; i < bytes.length; i++) {                byte b = bytes[i];                char c0 = sHexDigits[((b & 0xF0) >> 4)];                stringbuffer.append(c0);                char c1 = sHexDigits[(b & 0x0F)];                stringbuffer.append(c1);            }            return stringbuffer.toString();        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        }        return null;    }}

使用:

        String string = MD5Utils.getMD5String("123456789");

输出:

25f9e794323b453885f5181f1b624d0b
0 0
原创粉丝点击