MD5加密算法实现用户信息加密

MD5加密算法类:

public class MD5 {

  /**
  * MD5 加密
  * @param str
  * @author Red
  * @return
  */
 public final String getMD5(String str){
  MessageDigest md;
  try {
  md = MessageDigest.getInstance("MD5");//创建具有指定算法名称的摘要
  md.update(str.getBytes());//使用指定的字节数组更新摘要
  byte mdBytes[]=md.digest();//进行哈希计算并返回一个字节数组
  String hash="";
  for(int i=0;i<mdBytes.length;i++){ //循环字节数组
  int temp;
  if(mdBytes[i]<0) //如果小于0的字节,则转换为正数
  temp=256+mdBytes[i];
  else
  temp=mdBytes[i];
  if(temp<16)
  hash+="0";
  hash+=Integer.toString(temp, 16);//将字节转换为16进制后,转换为字符串
 }
  return hash;
 } catch (NoSuchAlgorithmException e) {
 // TODO Auto-generated catch block
  e.printStackTrace();
 }
return null;
  }
}

调用时,仅两行代码:(设User类为测试对象)

MD5 md5=new MD5();
user.setUserPwd(md5.getMD5(userPwd));

 

 

 

转载于:https://www.cnblogs.com/itred/p/3645717.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值