Java(springboot下)中加密解密的实现(通过自定义密钥并基于hutool工具包进行实现)

Java(springboot下)中加密解密的实现(通过自定义密钥并基于hutool工具包进行实现)

1、自定义封装的加密解密工具包

1.1、引入依赖
		<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.3</version>
        </dependency>
1.2、加密解密实现

加密(加密算法为对称加密ASE算法):

	//通过huTool工具包进行加密
    //key为自定义密钥
    public static String encrypt(String key,String encryptPassword){
        //生成密钥
        byte[] bytes = key.getBytes(StandardCharsets.UTF_8);
        //在密钥生成时必须为128/192/256 bits(位),本案例中使用256位
        //故需进行判断
        //byte,一字节,8位,故需要达到256位,需要32字节
        if (bytes.length!=32){
            //创建32字节的byte数组
            byte[] b = new byte[32];
            if (bytes.length<32){
                //将自定义密钥添加到b数组
                /**
                 * 方法:System.arraycopy
                 * 参数:
                 * src:the source array要插入的数组
                 * srcPos:starting position in the source array插入数组的起始位置
                 * dest:the destination array被插入的数组
                 * destPos:starting position in the destination data被插入数组插入时的起始位置
                 * length:the number of array elements to be copied要插入的数组的长度
                 */
                System.arraycopy(bytes,0,b,0,bytes.length);
            }
            bytes=b;
        }
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, bytes);
        //加密为16进制表示
        String encryptHex = aes.encryptHex(encryptPassword);
        return encryptHex;
    }

解密:

	//通过huTool工具包进行解密
    //key为自定义密钥
    public static String decrypt(String key,String decryptPassword){
        //生成密钥
        byte[] bytes = key.getBytes(StandardCharsets.UTF_8);

        if (bytes.length!=32){
            byte[] b = new byte[32];
            if (bytes.length<32){
                System.arraycopy(bytes,0,b,0,bytes.length);
            }
            bytes=b;
            System.out.println(bytes.length);
        }
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, bytes);
        //解密为字符串
        String decryptStr = aes.decryptStr(decryptPassword, CharsetUtil.CHARSET_UTF_8);
        return decryptStr;
    }

注:加密和解密传入的密钥key值必须相同

2、具体实现及测试结果

2.1、具体实现
package com.lingmeng.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.nio.charset.StandardCharsets;

public class PasswordUtils {



    //通过huTool工具包进行加密
    //key为自定义密钥
    public static String encrypt(String key,String encryptPassword){
        //生成密钥
        byte[] bytes = key.getBytes(StandardCharsets.UTF_8);
        //在密钥生成时必须为128/192/256 bits(位),本案例中使用256位
        //故需进行判断
        //byte,一字节,8位,故需要达到256位,需要32字节
        if (bytes.length!=32){
            //创建32字节的byte数组
            byte[] b = new byte[32];
            if (bytes.length<32){
                //将自定义密钥添加到b数组
                /**
                 * 方法:System.arraycopy
                 * 参数:
                 * src:the source array要插入的数组
                 * srcPos:starting position in the source array插入数组的起始位置
                 * dest:the destination array被插入的数组
                 * destPos:starting position in the destination data被插入数组插入时的起始位置
                 * length:the number of array elements to be copied要插入的数组的长度
                 */
                System.arraycopy(bytes,0,b,0,bytes.length);
            }
            bytes=b;
        }
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, bytes);
        //加密为16进制表示
        String encryptHex = aes.encryptHex(encryptPassword);
        return encryptHex;
    }


    //通过huTool工具包进行解密
    //key为自定义密钥
    public static String decrypt(String key,String decryptPassword){
        //生成密钥
        byte[] bytes = key.getBytes(StandardCharsets.UTF_8);
        if (bytes.length!=32){
            byte[] b = new byte[32];
            if (bytes.length<32){
                System.arraycopy(bytes,0,b,0,bytes.length);
            }
            bytes=b;
        }
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, bytes);
        //解密为字符串
        String decryptStr = aes.decryptStr(decryptPassword, CharsetUtil.CHARSET_UTF_8);
        return decryptStr;
    }

}

注:引入依赖后,代码直接复制即可使用

2.2、测试

测试代码:

@Test
    void Aes(){
        //加密密钥:root123
        //要加密的内容:root
        String encrypt = PasswordUtils.encrypt("root123", "root");
        System.out.println("root加密后的结果encrypt:"+encrypt);
        //解密密钥:root123
        //要解密的内容:同一密钥生成的加密字符串encrypt
        String root = PasswordUtils.decrypt("root123", encrypt);
        System.out.println("encrypt解密后的结果:" + root);

    }

测试结果:

root加密后的结果encrypt:a18adf155cdef669aa09990e93093604
encrypt解密后的结果:root
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落影离殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值