AES加解密工具类

前言

  • 当涉及到数据的安全性和保密性时,加密是一种关键的技术手段。AES(Advanced Encryption Standard)是一种广泛使用的对称加密算法,被认为是目前最安全和最常用的加密算法之一。

一、AES加解密工具类

package org.springblade.modules.data.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * AES 加解密工具
 *
 * @author kk
 */
public class AESUtil {

    static Logger logger = LoggerFactory.getLogger(AESUtil.class);
    // 密钥
    public static String key = "00B1E5452B9D453EC109D9B656150AF2";
    private static String charset = "utf-8";
    // 加密器类型:加密算法为AES,加密模式为CBC,补码方式为PKCS5Padding
    private static String transformation = "AES/ECB/PKCS5Padding";
    // 算法类型:用于指定生成AES的密钥
    private static String algorithm = "AES";

    /**
     * 加密
     */
    public static String encrypt(String content) {
        return encrypt(content, key);
    }

    /**
     * 解密
     */
    public static String decrypt(String content) {
        return decrypt(content, key);
    }

    /**
     * 加密
     *
     * @param content 需要加密的内容
     * @param key     加密密码
     * @return
     */
    public static String encrypt(String content, String key) {
        try {
            //构造密钥
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            //创建AES加密器
            Cipher cipher = Cipher.getInstance(transformation);
            byte[] byteContent = content.getBytes(charset);
            //使用加密器的加密模式
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            // 加密
            byte[] result = cipher.doFinal(byteContent);
            //使用BASE64对加密后的二进制数组进行编码
//            return new String(new Base64().encode(result));
            return Base64.getEncoder().encodeToString(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("{}", e);
        }
        return null;
    }

    /**
     * AES解密
     *
     * @param content 待解密内容
     * @param key     解密密钥
     * @return 解密之后
     * @throws Exception
     */
    public static String decrypt(String content, String key) {
        try {

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            Cipher cipher = Cipher.getInstance(transformation);
            //解密时使用加密器的解密模式
            cipher.init(Cipher.DECRYPT_MODE, skey);// 初始化
            byte[] result = cipher.doFinal(Base64.getMimeDecoder().decode(content));
            return new String(result); // 解密
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("{}", e);
        }
        return null;
    }

    public static void main(String[] args) {
//        String s = "{\"username\":\"aqgc\",\"password\":\"V@654321\"}";
        String s = "京A55645";
//        String encryptResultStr = encrypt(s, "kcc1YgEJZ%NLc%kY");
        String encryptResultStr = encrypt(s);
        // 加密
        System.out.println("加密前:" + s);
        System.out.println("加密后:" + encryptResultStr);
        // 解密
        System.out.println("解密后:" + decrypt(encryptResultStr));
        String base64Encode = Base64.getEncoder().encodeToString(s.getBytes());
        System.out.println("base64加密后:" + base64Encode);
        System.out.println("base64解密后:" + new String(Base64.getDecoder().decode(base64Encode)));
    }
}

二、AES加密简介(详细)


总结

如果此篇文章有帮助到您, 希望打大佬们能关注点赞收藏评论支持一波,非常感谢大家!
如果有不对的地方请指正!!!

参考1

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lfwh

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

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

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

打赏作者

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

抵扣说明:

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

余额充值