md5Util

Posted tanning

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了md5Util相关的知识,希望对你有一定的参考价值。

package com.rscode.credits.util;

/**
 * MD5加密类  
 * @author 12
 */
import java.security.MessageDigest;

public class MD5 {
    static char hexChars[] = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘,
            ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ };
    static char hexChars2[] = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘,
            ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ };
    /**
     * 32位大写
     * @param password
     * @return encryptPassword
     * @author 
     */
    public static String upperConvert32(String str) {
        try {
            byte[] bytes = str.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars[b >>> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 32位小写
     * @param password
     * @return encryptPassword
     * @author 
     */
    public static String lowerConvert32(String str) {
        try {
            byte[] bytes = str.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars2[b >>> 4 & 0xf];
                chars[k++] = hexChars2[b & 0xf];
            }
            return new String(chars);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 16位大写
     * @param s
     * @return
     */
    public String upperConvert16(String s) {
        String ns = upperConvert32(s).substring(8, 24);
        return ns;
    }
    /**
     * 16位小写
     * @param s
     * @return
     */
    public String lowerConvert16(String s) {
        String ns = lowerConvert32(s).substring(8, 24);
        return ns;
    }
//    public static void main(String[] args) {
//        System.out.println(MD5.convert32("12345"));
//    }
}

 

以上是关于md5Util的主要内容,如果未能解决你的问题,请参考以下文章

java MD5Utils.java

MD5Utils

md5Util

Md5Util

MD5Utils工具类模板

Md5Util加密工具类