MD5加密
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MD5 { private static final Logger LOG = LoggerFactory.getLogger(MD5.class); /** * MD5加密函数 * @param str 需要加密的字符串 * @return 返回加密后的信息 */ public static String crypt(String str) { if (str == null || str.length() == 0) { throw new IllegalArgumentException("String to encript cannot be null or zero length"); } StringBuffer hexString = new StringBuffer(); try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes()); byte[] hash = md.digest(); for (int i = 0; i < hash.length; i++) { if ((0xff & hash[i]) < 0x10) { hexString.append("0" + Integer.toHexString((0xFF & hash[i]))); } else { hexString.append(Integer.toHexString(0xFF & hash[i])); } } } catch (NoSuchAlgorithmException e) { LOG.error("md5加密出现异常", e); } return hexString.toString(); } }
版权声明:如需转载,请注明!PS:如是转载随便,请忽略