Hutool工具箱之Hutool-crypto加密解密

https://www.cnblogs.com/ssrs-wanghao/articles/17511089.html

一、对称加密
1.1 Aes加解密

package com.kuxinming.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class AesUtil {
//    长度只能是16位,24位,32位
    private static final String defaultKey = "1234567891011123";

    public static String encryptBase64(String content,String key){
        try{
            byte[] byteKey = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(),
                    key.getBytes()).getEncoded();
            SymmetricCrypto aes = SecureUtil.aes(byteKey);
            // 加密
            return aes.encryptBase64(content);
        }catch (Exception e){
            log.error(" 加密异常:{}",e.getMessage());
        }
        return null;
    }

    public static String decryptStr(String encryptString,String key){
        try{
            byte[] byteKey = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(),
                    key.getBytes()).getEncoded();
            SymmetricCrypto aes = SecureUtil.aes(byteKey);
            //解密
            return aes.decryptStr(encryptString);
        }catch (Exception e){
            log.error(" 解密异常:{}",e.getMessage());
        }
        return null;
    }



    public static void main(String[] args) {
        // 16位自定义密码
        String key = "1234567891011123";
        String content = "明文";
        String encryptStr = encryptBase64(content,key);
        System.out.println(decryptStr(encryptStr,key));
    }
}

二、不对称加密
2.1 Rsa加密

package com.kuxinming.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class RsaUtil {

    /**
     * 类型
     */
    public static final String ENCRYPT_TYPE = "RSA";

    /**
     * 获取公钥的key
     */
    private static final String PUBLIC_KEY = "RSAPublicKey";

    /**
     * 获取私钥的key
     */
    private static final String PRIVATE_KEY = "RSAPrivateKey";

    /**
     * 公钥加密
     *
     * @param content:
     * @param publicKey:
     * @author: 405
     * @date: 2021/6/28
     * @return: java.lang.String
     */
    public static String encrypt(String content, PublicKey publicKey) {
        try {
            RSA rsa = new RSA(null, publicKey);
            return rsa.encryptBase64(content, KeyType.PublicKey);
        } catch (Exception e) {
            log.error("公钥加密异常 msg:{}",e.getMessage());
        }
        return null;
    }

    /**
     * 公钥加密
     *
     * @param content:
     * @param publicKey:
     * @author: 405
     * @date: 2021/6/28
     * @return: java.lang.String
     */
    public static String encrypt(String content, String publicKey) {
        try {
            RSA rsa = new RSA(null, publicKey);
            return rsa.encryptBase64(content, KeyType.PublicKey);
        } catch (Exception e) {
            log.error("公钥加密异常 msg:{}",e.getMessage());
        }
        return null;
    }


    /**
     * 私钥解密
     *
     * @param content:
     * @param privateKey:
     * @author: 405
     * @date: 2021/6/28
     * @return: java.lang.String
     */
    public static String decrypt(String content, PrivateKey privateKey) {
        try {
            RSA rsa = new RSA(privateKey, null);
            return rsa.decryptStr(content, KeyType.PrivateKey);
        } catch (Exception e) {
            log.error("私钥解密异常 msg:{}",e.getMessage());
        }
        return null;
    }

    /**
     * 私钥解密
     *
     * @param content:
     * @param privateKey:
     * @author: 405
     * @date: 2021/6/28
     * @return: java.lang.String
     */
    public static String decrypt(String content, String privateKey) {
        try {
            RSA rsa = new RSA(privateKey, null);
            return rsa.decryptStr(content, KeyType.PrivateKey);
        } catch (Exception e) {
            log.error("私钥解密异常 msg:{}",e.getMessage());
        }
        return null;
    }

    /**
     * 获取公私钥-请获取一次后保存公私钥使用
     * @return
     */
    public static Map<String,String> generateKeyPair() {
        try {
            KeyPair pair = SecureUtil.generateKeyPair(ENCRYPT_TYPE);
            PrivateKey privateKey = pair.getPrivate();
            PublicKey publicKey = pair.getPublic();
            // 获取 公钥和私钥 的 编码格式(通过该 编码格式 可以反过来 生成公钥和私钥对象)
            byte[] pubEncBytes = publicKey.getEncoded();
            byte[] priEncBytes = privateKey.getEncoded();

            // 把 公钥和私钥 的 编码格式 转换为 Base64文本 方便保存
            String pubEncBase64 = new BASE64Encoder().encode(pubEncBytes);
            String priEncBase64 = new BASE64Encoder().encode(priEncBytes);

            Map<String, String> map = new HashMap<String, String>(2);
            map.put(PUBLIC_KEY,pubEncBase64);
            map.put(PRIVATE_KEY,priEncBase64);

            return map;
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        System.out.println(JSONObject.toJSONString(generateKeyPair()));
//        String ftpUrl = "ftp://wasu:234@125.2321321he12321321";
//        String content = ftpUrl;
//        encrypt(content,"");
    }
}

三、多重加密
3.1 Aes和Rsa加密

package com.wang.util;

public class SecureUtil {

    public static String aesEncrypt(String content,String key)
            throws Exception{
        return AesUtil.encryptBase64(content, key);
    }

    public static String aesDecrypt(String content,String key)
            throws Exception{
        return AesUtil.decryptStr(content, key);
    }
    public static String rsaEncrypt(String content,String publicKey)
            throws Exception{
        return RsaUtil.encrypt(content,publicKey);
    }
    public static String rsaDecrypt(String content,String privateKey)
            throws Exception{
        return RsaUtil.decrypt(content,privateKey);
    }

    public static String aesAndRsaEncrypt(String content,String aseKey
            ,String publicKey) throws Exception{
        String aesDecryptVal = aesEncrypt(content,aseKey);
        return rsaEncrypt(aesDecryptVal,publicKey);
    }
    public static String rsaAndAesDecrypt(String content,String aseKey
            ,String privateKey) throws Exception{
        String aseDecryptVal = rsaDecrypt(content,privateKey);
        return aesDecrypt(aseDecryptVal,aseKey);
    }

    public static String rsaAndAesEncrypt(String content,String aseKey
            ,String publicKey)  throws Exception{
        String rsaEncryptVal = rsaEncrypt(content,publicKey);
        return aesEncrypt(rsaEncryptVal,aseKey);
    }
    public static String aesAndRsaDecrypt(String content,String aseKey
            ,String privateKey)  throws Exception{
        String aseDecryptVal = aesDecrypt(content,aseKey);
        return  rsaDecrypt(aseDecryptVal,privateKey);
    }
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值