AES加密工具

该博客介绍了如何在Java中使用Hutool库和标准Java Cryptography API进行AES加密和解密操作。示例代码展示了如何创建密钥、初始化密码器以及执行加密和解密的过程。同时,还提供了将字节转换为16进制字符串的方法。
摘要由CSDN通过智能技术生成
package com.mtkj.common.bpqutils;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;


public class AESUtils {



    /**
     * @Description:AES 加密
     */
    public static byte[] encrypt(String content, String password) {

        try {
            SecretKey key = new SecretKeySpec(HexUtil.decodeHex(password), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/NOPadding"); // 创建密码器
            byte[] byteContent = parseHexStr2Byte(content);
            cipher.init(Cipher.ENCRYPT_MODE, key); // 初始化
            assert byteContent != null;
            return cipher.doFinal(byteContent);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | InvalidKeyException | IllegalBlockSizeException e) {
            e.printStackTrace();
        }

        return  null;
    }

    /**
     * @Description:AES 解密
     */
    public static byte[] decrypt(byte[] content, String password) {

        try {
            SecretKeySpec key = new SecretKeySpec(parseHexStr2Byte(password), "AES");
            // 创建密码器
            Cipher cipher = Cipher.getInstance("AES/ECB/NOPadding");
            // 初始化
            cipher.init(Cipher.DECRYPT_MODE, key);
            // 加密
            return cipher.doFinal(content);

        } catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException | NoSuchPaddingException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**将二进制转换成16进制
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**将16进制转换为二进制
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length()/2];
        for (int i = 0;i< hexStr.length()/2; i++) {
            int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
            int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static void main (String [] args) throws NoSuchAlgorithmException, ParseException {
        String content = "2075907589680002115F8252031809476EA5E4E1852373402920210709101300";
        
        //Hutool
        //加密
        SecretKey secretKey = SecureUtil.generateKey("AES", HexUtil.decodeHex("451FFA7B7CA7047BB51BB5F62FFA658BC8EC065CAABEB92857F0238DB62826E5"));
        AES aes = new AES(Mode.ECB, Padding.NoPadding, secretKey);
        String encrypt1 = aes.encryptHex(HexUtil.decodeHex(content));
        System.out.println("加密后:" + encrypt1);
        byte[] decrypt2 = aes.decrypt(encrypt1);
        System.out.println("解密后:" +HexUtil.encodeHexStr(decrypt2));



        byte[] encryptResult = encrypt(content, "451FFA7B7CA7047BB51BB5F62FFA658BC8EC065CAABEB92857F0238DB62826E5");
        String encryptResultStr1 =  parseByte2HexStr(encryptResult);
        System.out.println("加密后:" + encryptResultStr1);
        byte[] decrypt = decrypt(encryptResult, "451FFA7B7CA7047BB51BB5F62FFA658BC8EC065CAABEB92857F0238DB62826E5");
        System.out.println("解密后:" +parseByte2HexStr(decrypt));
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值