欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AesUtil数据加密解密工具类

程序员文章站 2022-03-12 20:57:45
...

概述

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

代码

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:[[email protected]
byte转回16进制串:827382163821
原字串:827382163821
加密后:X0GYhLRQl/YU/WcbiOSymw==
解密:827382163821