标签:except new dig print length port uri nbsp md5加密
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")); // } }
标签:except new dig print length port uri nbsp md5加密
原文地址:https://www.cnblogs.com/tanning/p/10243923.html