Java实现MD5

本文摘自:http://www.cnblogs.com/yanghuahui/archive/2013/06/16/3139411.html


经实践检测可行,以备以后使用。

import java.security.MessageDigest;

/**
 * Created by Administrator on 2016/11/26.
 */
public class test {
    public static void main(String[] args) {
           MD5("test");
    }

    public static String MD5(String basicInfo){
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] strTemp = basicInfo.getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(strTemp);
            byte[] md = md5.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}



你可能感兴趣的:(WEB开发)