2023-05-05
JAVA
0

目录

基于hutool造一个加解密轮子
前提
相关代码

基于hutool造一个加解密轮子

前提

  • 搭建springboot项目
  • 引入hutool依赖
  • 建议引入lombok依赖

相关代码

  1. 依赖
xml
<!-- 注意修改版本信息 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${最新版本}</version> </dependency>
  1. 生成密钥代码
java
/** * 生成对称密钥 * * @return {@link String} */ public static String genKey() { // 对称加密,随机生成128位加密密钥, // 注,密钥长度必须是二进制的128位/192位或256位,jdk默认是128位,如果要使用更高位加密, // 请先下载并安装 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files byte[] keyBytes = SecureUtil.generateKey("AES").getEncoded(); return HexUtil.encodeHexStr(keyBytes); } /** * 生成非对称密钥对 * * @return {@link Map}<{@link String}, {@link String}> */ public static Map<String, String> genKeyPair() { Map<String, String> result = Maps.newHashMap(); KeyPair keyPair = SecureUtil.generateKeyPair("RSA", 2048); result.put("pub_key", Base64.encode(keyPair.getPublic().getEncoded())); result.put("pri_key", Base64.encode(keyPair.getPrivate().getEncoded())); return result; }
  1. 完整工具类
java
package com.code.generator.util; import cn.hutool.core.codec.Base64; import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.RSA; import cn.hutool.crypto.symmetric.AES; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import java.nio.charset.StandardCharsets; import java.security.KeyPair; import java.util.Map; /** * SEC Util * 加密相关工具 * * @author JACK WEI * @date 2024/01/14 */ @Slf4j public class SecUtil { // 该值由于生成密钥代码产生,建议放在配置文件中 private final static String KEY = "61D4B243CB93511D5238BC1BABA17C61"; private final static AES aes = new AES(KEY.getBytes(StandardCharsets.UTF_8)); private final static String PRI_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDE8v/8sFBr/+b/UM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6jMHGX1t7bdwr80d3nKmAfhWQ2ZBlzywwmRhYROxXb/rr17nyY7kHzcfW0Ud0p2X7yK14gvGexQNb2rbzL/DkIxcTBifi6EKetvSldFXseywRL+NRA9NwdeUNqRQKBgQCGqjSBvTBpeLdSzXqEwd7xqfKPND8NcSQ+raUfrc0q5U2r/Vw7hA/HYWraTRkiWiCgCEdKpdJ47jZVU3K/irk/aIBSw1f3/wshmYyIP4t+1YtkbA0HiCu0bwHui9RvibgVG8tA7Zb3banL1ewmpMvYEitD/rYmmm9fv1T1Kx0GIQKBgCGYr7fTugsJZwNz+xVQvUsxVf4kkB174mcqcDbArlUMyctpg7n3y4YJMx/cLQv1/gSiJbfBm7Ao5KtQvQoQGBuV+WIvKpM8CxBaeIJmT3inux8zSv6ucafwK/VRUJfNmVD2CeXXX14lqfxTI0MYBYqtBp3UU8NrhDvuZHAwqWoz"; private final static String PUB_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxPL//LBQa//m/1DDRBGVNxkKjyFBtzJT/3g3WWy5TzMyy+Jj4LX/1CdD8GOX2SMB2Bq9ek1UhcLNtzwKuNl24Yr/Run+nse9F4SC+rBO4muj/ZWzka7fbD8cn5pZFFcwGJC2tMBwNyEJbl2oAWgKAVe/xd4qlz5bxj1unPWsvNuk0535TiS/9euK/rmxZE8UV+nSDeUo6JgZ9QmcswhXrNiJA9SX1lN9isYfwfKM4sfUfpBKlKUpRMyMR6svsKV71RqJbIiwGY1zoOIf/nAo8iLKPPqIrPMDj24LeFcb234prU04KWnOdnELU8PAmDdA2T+Jqzn6k6iDXLiTZIC5BQIDAQAB"; private final static RSA rsa = new RSA(PRI_KEY, PUB_KEY); /** * 生成对称密钥 * * @return {@link String} */ public static String genKey() { // 对称加密,随机生成128位加密密钥, // 注,密钥长度必须是二进制的128位/192位或256位,jdk默认是128位,如果要使用更高位加密, // 请先下载并安装 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files byte[] keyBytes = SecureUtil.generateKey("AES").getEncoded(); return HexUtil.encodeHexStr(keyBytes); } /** * 生成非对称密钥对 * * @return {@link Map}<{@link String}, {@link String}> */ public static Map<String, String> genKeyPair() { Map<String, String> result = Maps.newHashMap(); KeyPair keyPair = SecureUtil.generateKeyPair("RSA", 2048); result.put("pub_key", Base64.encode(keyPair.getPublic().getEncoded())); result.put("pri_key", Base64.encode(keyPair.getPrivate().getEncoded())); return result; } /** * aes加密 * * @param str 需要加密的字符串 * @return {@link String } * @author JACK * @date 2023/05/05 */ public static String encAes(String str) { return aes.encryptBase64(str); } /** * rsa加密 * * @param str 需要加密的字符串 * @return {@link String } * @author JACK * @date 2023/05/05 */ public static String encRsa(String str) { return rsa.encryptBase64(str, KeyType.PublicKey); } /** * aes解密 * * @param str 需要解密的字符串 * @return {@link String } * @author JACK * @date 2023/05/05 */ public static String decAes(String str) { String decrypted = StrUtil.EMPTY; try { decrypted = aes.decryptStr(str); } catch (Exception e) { log.error("解密失败", e); } return decrypted; } /** * rsa解密 * * @param str 需要解密的字符串 * @return {@link String } * @author JACK * @date 2023/05/05 */ public static String decRsa(String str) { return rsa.decryptStr(str, KeyType.PrivateKey); } }

温馨提示

AES密钥切勿随意赋值,需要遵从AES的128位、192位或256位长度要求,默认为128位。如果是jdk17应用次工具最好在启动类的主方法末尾添加一下代码:SecureUtil.disableBouncyCastle();起作用是强制关闭Bouncy Castle库的使用,全局有效