加密工具类

非对称加密

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.lang3.StringUtils;

public class MD5Util {

	public static String getMd5Str(String inParam) {
		final String SALT = "XXtest$99#";
		//inParam += SALT; 
		String outVal = "";
		if(StringUtils.isBlank(inParam)) {
			System.out.println("参数为空");
		}
		try {
			//MessageDigest 类为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法。
			//信息摘要是安全的单向哈希函数,它接收 任意大小的数据,并输出固定长度的哈希值。
			//MessageDigest 对象开始被初始化。
			MessageDigest mDigest = MessageDigest.getInstance("MD5");
			//通过使用 update 方法处理数据
			mDigest.update(inParam.getBytes());
			//调用 digest 方法之一完成哈希计算同时将Byte数组转换成16进制
			outVal = bytesToHexString(mDigest.digest());
			
			System.out.println(outVal);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return outVal;
	}
	
	private static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        //MD5加密后bytes长度16转换成32位16进制字符串
        for (int i = 0; i < bytes.length; i++) {
        	/**
        	 * 在32位的电脑中数字都是以32格式存放的,如果是一个byte(8位)类型的数字,
        	 * 他的高24位里面都是随机数字,低8位才是实际的数据。
        	 * java.lang.Integer.toHexString() 方法的参数是int(32位)类型.
        	 * 如果输入一个byte(8位)类型的数字,这个方法会把这个数字的高24为也看作有效位,
        	 * 这就必然导致错误,使用& 0XFF操作,可以把高24位置0以避免这样错误.
        	 * 
        	 * 0xFF = 1111 1111  低8位为1,高位都为0
        	 * 故 &0xFF 可将数字的高位都置为0,低8位不变
        	 * 
        	 * */
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}

对称加密

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESBaseUtil {
    public static String DES = "AES"; // optional value AES/DES/DESede

    public static String CIPHER_ALGORITHM = "AES"; // optional value AES/DES/DESede


    public static Key getKey(String strKey) {
        try {
            if (strKey == null) {
                strKey = "";
            }
            KeyGenerator _generator = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(strKey.getBytes());
            _generator.init(128, secureRandom);
            return _generator.generateKey();
        } catch (Exception e) {
            throw new RuntimeException(" 初始化密钥出现异常 ");
        }
    }

    // 加密 ----先 AES 再 BASE64
    public static String encrypt(String data, String key) throws Exception {
        SecureRandom sr = new SecureRandom();
        Key secureKey = getKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secureKey, sr);
        byte[] bt = cipher.doFinal(data.getBytes());
        String strS = new BASE64Encoder().encode(bt);
        return strS;
    }

    // 解密 ----先BASE64  再 AES
    public static String decrypt(String message, String key) throws Exception {
        SecureRandom sr = new SecureRandom();
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        Key secureKey = getKey(key);
        cipher.init(Cipher.DECRYPT_MODE, secureKey, sr);
        byte[] res = new BASE64Decoder().decodeBuffer(message);
        res = cipher.doFinal(res);
        return new String(res);
    }
  
}

加密和解密中的参数 key ,就是盐,可以写在该工具类,也可另外写在业务类,不同的业务用不同的盐

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值