MD5加盐

package com.zqz.simple_demo.common;

import org.springframework.util.DigestUtils;

/**
 * @Author: Tom_Z
 * @Data: 2019/4/25 13:50
 * @Description:
 **/
public class MD5Util {
    //普通MD5
    public String encodeByMD5(String string) throws Exception {
        String res = DigestUtils.md5DigestAsHex(string.getBytes());
        return res;
    }
    //自定义的加盐MD5算法
    public String encodeByMD5WithSalt(String string) throws Exception {
        String md5 = DigestUtils.md5DigestAsHex(string.getBytes());
        char[] chars = md5.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]>=48&&chars[i]<=57){
                chars[i]=(char)(105-chars[i]);
            }
        }
        return "".valueOf(chars);
    }
    //去盐MD5
    public String encodeByMD5WithSaltTransferToNormal(String resWithSalt) throws Exception {
        char[] chars = resWithSalt.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]>=48&&chars[i]<=57){
                chars[i]=(char)(105-chars[i]);
            }
        }
        return "".valueOf(chars);
    }
    //测试一下
    public static void main(String[] args) {
        try {
            MD5Util md5Util = new MD5Util();
            String res = md5Util.encodeByMD5("123456");
            System.out.println("普通MD5:\n\t"+res);

            String resWithSalt = md5Util.encodeByMD5WithSalt("123456");
            System.out.println("加盐MD5:\n\t"+resWithSalt);

            String resWithOutSalt = md5Util.encodeByMD5WithSaltTransferToNormal(resWithSalt);
            System.out.println("去盐恢复普通MD5:\n\t"+resWithOutSalt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

结果:

普通MD5:
	e10adc3949ba59abbe56e057f20f883e
加盐MD5:
	e89adc6050ba40abbe43e942f79f116e
去盐恢复普通MD5:
	e10adc3949ba59abbe56e057f20f883e

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值