2022-04-13 JDK 1.8 自带 Crypto 加密封装工具类

JDK 1.8 自带 Crypto 加密封装工具类

工具类

/**
 * @Author zsl
 * @Date 2022/1/26 10:16
 * @Email 249269610@qq.com
 */
public class CryptoUtil {
    private static final String ALGORITHM = "DESede"; // 使用的加密算法
    private static final String SECRET = "PUBLICSTATICVOIDMAINSTRINGARGS";    // 加密的盐

    /**
     * 对明文加密
     * @param encode 明文
     * @return 加密后的密文字节数组
     */
    public static byte[] encode(byte[] encode) {
        // 加密后的密文
        byte[] result = new byte[0];
        try {
            // 获取实例,设置算法
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // 初始化,设置加密模式、盐
            cipher.init(Cipher.ENCRYPT_MODE, getKey(SECRET));
            result = cipher.doFinal(encode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 对明文加密并转Base64
     * @param encode 待加密的明文
     * @return 加密后的Base64密文
     */
    public static String encode2Base64(String encode) {
        return Base64.getEncoder().encodeToString(encode(encode.getBytes()));
    }

    /**
     * 获取密钥
     * @param key 盐
     * @return
     */
    public static Key getKey(String key) {
//        return new SecretKeySpec(key.getBytes(), ALGORITHM);
        return getDESedeKey(key.getBytes());
    }

    public static Key getDESedeKey(byte[] key) {
        Key secretKey = null;
        try {
            DESedeKeySpec spec = new DESedeKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
            secretKey = keyFactory.generateSecret(spec);
        }catch (Exception e){
            e.printStackTrace();
        }
        return secretKey;
    }

    /**
     * 对密文解密
     * @param decode 密文
     * @return
     */
    public static byte[] decode(byte[] decode) {
        byte[] result = new byte[0];
        try {
            // 获取实例,设置算法
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // 初始化,设置解密模式、盐
            cipher.init(Cipher.DECRYPT_MODE, getKey(SECRET));
            // 解密
            result = cipher.doFinal(decode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Base64密文解密
     * @param baseDecode Base64编码密文
     * @return
     */
    public static String decodeBase64(String baseDecode) {
        // Base64解码 (转String会出现填充的问题导致解密出错)
        byte[] decode = Base64.getDecoder().decode(baseDecode.getBytes());
        // 解密
        byte[] bytes = decode(decode);
        return new String(bytes);
    }

    public static void main(String[] args) {
        String content = "";
        // 对明文进行加密
        System.out.println("encode: " + new String(CryptoUtil.encode(content.getBytes())));
        // 对明文进行加密并转base64编码
        System.out.println("encode2Base64: " + CryptoUtil.encode2Base64(content));

        String secret = "";
        // Base64密文解密
        System.out.println("decodeBase64: " + CryptoUtil.decodeBase64(secret));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值