加密工具类

package com.hengyu.ticket.util;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.Key;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Formatter;
import java.util.UUID;

public class SecurityHanlder {

    //加密类型
    public enum KeyType{
        AES,DES,DESede
    }

    private static final String uuid = "4c7a37a8-b8d7-442f-b9df-31ec4bcacebc";

    public static String TYPE = KeyType.AES.name(); // optional value AES/DES/DESede

    /**
     * 获取 md5 加密字符串
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String md5(String str) throws Exception {
        return getSign(str, "MD5");
    }

    /**
     * 获取 md5 加密字符串
     * 规则 str+uuid
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String md5x(String str) throws Exception {
        return getSign(str + uuid, "MD5");
    }

    /**
     * 获取 sha1 加密字符串
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String sha1(String str) throws Exception {
        return getSign(str, "SHA-1");
    }

    /**
     * 获取 sha1 加密字符串
     * 二次加密
     * 规则 str+uuid
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String sha1x(String str) throws Exception {
        return getSign(str + uuid, "SHA-1");
    }

    /**
     * 获取 sha1进行加密的字符后再进行 md5  加密字符串
     * 二次加密
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String md5_sha1(String str) throws Exception {
        return getSign(getSign(str, "MD5"), "SHA-1");
    }

    /**
     * 获取 md5 进行加密的字符后再进行 sha1加密字符串
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String sha1_md5(String str) throws Exception {
        return getSign(getSign(str, "SHA-1"), "MD5");
    }

    /**
     * 创建随机字符
     *
     * @return
     */
    public static String createNonceStr() {
        return UUID.randomUUID().toString();
    }

    /**
     * 获取加密签名
     *
     * @param str  字符
     * @param type 加密类型
     * @return
     * @throws Exception
     */
    public static String getSign(String str, String type) throws Exception {
        MessageDigest crypt = MessageDigest.getInstance(type);
        crypt.reset();
        crypt.update(str.getBytes("UTF-8"));
        return str = byteToHex(crypt.digest());
    }

    /**
     * 字节转换 16 进制
     *
     * @param hash
     * @return
     */
    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    /**
     * base64编码
     *
     * @param source
     * @return
     */
    public static String Base64Encoder(String source) {
        return new String(Base64.getEncoder().encode(source.getBytes()));
    }



    public static Key getSecretKey(String key) throws Exception{
        SecretKey securekey = null;
        if(key == null){
            key = "";
        }
        KeyGenerator keyGenerator = KeyGenerator.getInstance(TYPE);
        keyGenerator.init(new SecureRandom(key.getBytes()));
        securekey = keyGenerator.generateKey();
        return securekey;
    }

    /**
     * 文本加密
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static String encrypt(String data,String key) throws Exception {
        SecureRandom sr = new SecureRandom();
        Key securekey = getSecretKey(key);
        Cipher cipher = Cipher.getInstance(TYPE);
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        byte[] bt = cipher.doFinal(data.getBytes());
        String strs = Base64.getEncoder().encodeToString(bt);
        return strs;
    }


    /**
     * 文本解密
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static String detrypt(String message,String key) throws Exception{
        SecureRandom sr = new SecureRandom();
        Cipher cipher = Cipher.getInstance(TYPE);
        Key securekey = getSecretKey(key);
        cipher.init(Cipher.DECRYPT_MODE, securekey,sr);
        byte[] res = Base64.getDecoder().decode(message);
        res = cipher.doFinal(res);
        return new String(res);
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值