| 123456789101112131415161718192021222324252627282930313233343536373839 |
- package com.usky.utils;
- import java.io.UnsupportedEncodingException;
- import java.math.BigInteger;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public class MD5Util {
- public final static String MD5(String s){
- char hexDigits[] ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- try{
- byte[] btInput = s.getBytes();
- MessageDigest mdInst = MessageDigest.getInstance("MD5");
- mdInst.update(btInput);
- byte[] md = mdInst.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){
- return null;
- }
- }
- public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
- byte[] secretBytes = null;
- MessageDigest md5=MessageDigest.getInstance("MD5");
- md5.update(str.getBytes());
- secretBytes = md5.digest();
- String newstr = new BigInteger(1,secretBytes).toString(16);
- for(int i=0;i<32-newstr.length();i++)
- newstr = "0"+newstr;
- return newstr;
- }
- }
|