AesUtil数据加密解密工具类

概述

项目中有时会用到数据加密解密的需求,特别是对接第三方的接口中。这个工具类总结一些加密解密的方法。
(未完待续。。。)

代码

package com.wzh.utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * AES工具类
 * @author weizhenhui
 * @date 2021/4/30 14:37
 */
public class AesUtil {
    private static final String CHARSET_NAME = "UTF-8";
    private static final String KEY_ALGORITHM = "AES";
    private static final String CIPHER_ALGORITHM = "AES/CBC/NoPadding";

    private AesUtil() {
    }

    /**
     * 生成加密key
     */
    public static byte[] getEncryptKey() {
        KeyGenerator keyGenerator;
        // 实例化一个用AES加密算法的密钥生成器
        try {
            keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("NoSuchAlgorithmException=>" + e.getMessage());
        }
        // bitSize
        keyGenerator.init(128);
        // 生成一个密钥。
        SecretKey key = keyGenerator.generateKey();
        return key.getEncoded();
    }

    /**
     * aes加密-128位
     *
     * @param content  待加密内容
     * @param keyBytes 加密密钥
     * @return 16进制加密数据
     */
    public static String encrypt(String content, byte[] keyBytes) {
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = content.getBytes(CHARSET_NAME);
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec keyspec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
            IvParameterSpec ivspec = new IvParameterSpec(keyBytes);
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception e) {
            throw new UnsupportedOperationException("AES加密异常", e);
        }
    }

    /**
     * 校验密钥
     *
     * @param key 32位长度的数字字符串密钥
     */
    private static byte[] getKeyBytes(String key) {
        if (key == null || key.length() == 0) {
            throw new IllegalArgumentException("密钥不能为空");
        }
        byte[] keyBytes = hex2Bytes(key);
        if (keyBytes.length != 16) {
            throw new IllegalArgumentException("密钥长度为16位");
        }
        return keyBytes;
    }

    /**
     * aes加密-128位
     *
     * @param content 待加密内容
     * @param key     加密密钥
     * @return 16进制加密数据
     */
    public static String encryptByKey(String content, String key) {
        byte[] keyBytes = getKeyBytes(key);
        return encrypt(content, keyBytes);
    }

    /**
     * aes解密-128位
     *
     * @param encryptContent 待解密内容
     * @param key            解密密钥
     */
    public static String decryptByKey(String encryptContent, String key) {
        byte[] keyBytes = getKeyBytes(key);
        return decrypt(encryptContent, keyBytes);
    }

    /**
     * aes解密-128位
     *
     * @param encryptContent 待解密内容
     * @param keyBytes       解密密钥
     */
    public static String decrypt(String encryptContent, byte[] keyBytes) {
        try {
            byte[] encrypted1 = Base64.getDecoder().decode(encryptContent);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            SecretKeySpec keyspec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
            IvParameterSpec ivspec = new IvParameterSpec(keyBytes);
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] original = cipher.doFinal(encrypted1);
            String value = new String(original, CHARSET_NAME).trim();
            return (value == null || value.trim().length() == 0) ? encryptContent : value;
        } catch (Exception e) {
            throw new UnsupportedOperationException("AES解密异常", e);
        }
    }

    /**
     * 将byte[] 转换成字符串
     */
    public static String byte2Hex(byte[] sourceBytes) {
        StringBuilder hexBuilder = new StringBuilder();
        for (byte b : sourceBytes) {
            String hexString = Integer.toHexString(0x00ff & b);
            hexBuilder.append(hexString.length() == 1 ? 0 : "").append(hexString);
        }
        return hexBuilder.toString();
    }

    /**
     * 将16进制字符串转为转换成字符串
     */
    public static byte[] hex2Bytes(String source) {
        byte[] sourceBytes = new byte[source.length() / 2];
        for (int i = 0; i < sourceBytes.length; i++) {
            sourceBytes[i] = (byte) Integer.parseInt(source.substring(i * 2, i * 2 + 2), 16);
        }
        return sourceBytes;
    }

}

以上都是JDK自带的功能,不需要引其他的依赖。

使用案例

import com.wzh.utils.AesUtil;

/**
 * AesUtil 测试
 *
 * @author weizhenhui
 * @date 2021/4/30 14:44
 */
public class AesUtilTest {
    public static void main(String[] args) {
        // 任意字符串
        String str = "qwertyuiop";
        byte[] keyBytes = AesUtil.getEncryptKey();
        String encryptStr = AesUtil.encrypt(str, keyBytes);
        System.out.println("原字串:" + str);
        System.out.println("加密后:" + encryptStr);
        System.out.println("解密:" + AesUtil.decrypt(encryptStr, keyBytes));

        // 16进制字数符串
        String string = "827382163821";
        byte[] hex2Bytes = AesUtil.hex2Bytes(string);
        System.out.println("原字串:" + string);
        System.out.println("16进制串转byte:" + hex2Bytes);
        System.out.println("byte转回16进制串:" + AesUtil.byte2Hex(hex2Bytes));

        // 需要32位长度的数字key
        String key = "20210430143145002021043014314500";
        String encryptKeyString = AesUtil.encryptByKey(string, key);
        System.out.println("原字串:" + string);
        System.out.println("加密后:" + encryptKeyString);
        System.out.println("解密:" + AesUtil.decryptByKey(encryptKeyString, key));
    }
}

运行结果:

原字串:qwertyuiop
加密后:4x7LMGRsEK7Mk01Uf3A/AA==
解密:qwertyuiop
原字串:827382163821
16进制串转byte:[B@233c0b17
byte转回16进制串:827382163821
原字串:827382163821
加密后:X0GYhLRQl/YU/WcbiOSymw==
解密:827382163821
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值