标签:
package util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Crypter { public static String md5Encrypt(String originalString){ StringBuffer sb = new StringBuffer(); //if((null != str) || (0 != (str.trim().length())) || !"null".equals(str)) if(StringHelper.isNotBlank(originalString)){ try{ MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(originalString.getBytes()); byte[] b = md5.digest(); for (int i = 0; i < b.length; i++) { int v = b[i]; v = v < 0 ? 256 + v : v; String cc = Integer.toHexString(v); if (cc.length() == 1) sb.append(‘0‘); sb.append(cc); } }catch (NoSuchAlgorithmException e) { return null; } } return sb.toString(); } }
StringHelper类也是一个单独的工具类,MD5编码这个类用到了判断字符串不为空这个类
标签:
原文地址:http://www.cnblogs.com/JAYIT/p/5166484.html