MD5加密
MD5加密工具:
1.原料:
引入jar包:commons-codec-1.9.jar(commons-codec.jar)
2.代码:
package test; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.digest.DigestUtils; import sun.misc.BASE64Encoder; public class MD5Utils { public static void main(String[] args) { String msg = "123222"; System.out.println(encrypt(msg)); System.out.println(md5Hex(msg)); } //得到MD5消息摘要,并用BASE64编码成可显示字符串. public static String encrypt(String message){ try { MessageDigest md = MessageDigest.getInstance("md5"); byte md5[] = md.digest(message.getBytes()); BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(md5); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } /** * md5加密 * * @param value 要加密的值 * @return md5加密后的值 */ public static String md5Hex(String value) { return DigestUtils.md5Hex(value); } }
输出结果:
6igbR8txHaQYw1sHufBs1Q==
ea281b47cb711da418c35b07b9f06cd5