md5加密-----直接可用源码


package com.yd.xcxdkh.login.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;

public class MD5Encryption {
/**
* 对字符串md5加密
*
* @param str   //用户输入进来的密码
* @return   //返回加密后的密码
* @throws Exception
*/
public static String getMD5(String str) throws Exception {
try {
System.out.println("str=="+str);
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(str.getBytes());
// digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
String value = new BigInteger(1, md.digest()).toString(16);
System.out.println(value);
return value;
} catch (Exception e) {
throw new Exception("MD5加密出现错误");
}
}


public static void main(String[] args) throws Exception {
String md5 = getMD5("asdf");
System.out.println(md5);
}



}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM