All Downloads are FREE. Search and download functionalities are using the official Maven repository.

club.zhcs.utils.codec.AES Maven / Gradle / Ivy

package club.zhcs.utils.codec;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.nutz.lang.Lang;
import org.nutz.repo.Base64;

import lombok.experimental.UtilityClass;

/**
 * 
 * @author kerbores
 *
 */
@UtilityClass
public class AES {

    static final String ALGORITHM = "AES/ECB/PKCS5Padding";
    static final Charset CHAR_SET = StandardCharsets.UTF_8;
    static final String DEFAULT_KEY = genKey();

    public static Key createKey() {
        try {
            // 生成key
            KeyGenerator keyGenerator;
            // 构造密钥生成器,指定为AES算法,不区分大小写
            keyGenerator = KeyGenerator.getInstance("AES");
            // 生成一个128位的随机源,根据传入的字节数组
            keyGenerator.init(128);
            // 产生原始对称密钥
            SecretKey secretKey = keyGenerator.generateKey();
            // 获得原始对称密钥的字节数组
            byte[] keyBytes = secretKey.getEncoded();
            // key转换,根据字节数组生成AES密钥
            return new SecretKeySpec(keyBytes, "AES");
        } catch (NoSuchAlgorithmException e) {
            throw Lang.wrapThrow(e);
        }

    }

    public static String genKey() {
        try {
            // 生成key
            KeyGenerator keyGenerator;
            // 构造密钥生成器,指定为AES算法,不区分大小写
            keyGenerator = KeyGenerator.getInstance("AES");
            // 生成一个128位的随机源,根据传入的字节数组
            keyGenerator.init(128);
            // 产生原始对称密钥
            SecretKey secretKey = keyGenerator.generateKey();
            // 获得原始对称密钥的字节数组
            byte[] keyBytes = secretKey.getEncoded();
            // key转换,根据字节数组生成AES密钥
            return Base64.encodeToString(keyBytes, false);
        } catch (NoSuchAlgorithmException e) {
            throw Lang.wrapThrow(e);
        }

    }

    public static Key loadKey(String key) {
        return new SecretKeySpec(Base64.decode(key), "AES");
    }

    public static String encrypt(String context) {
        return encrypt(context, DEFAULT_KEY);
    }

    public static String encrypt(String context, String key) {
        return encrypt(context, loadKey(key));
    }

    public static String encrypt(String context, Key key) {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            // 将加密并编码后的内容解码成字节数组
            byte[] result = cipher.doFinal(context.getBytes());
            return Base64.encodeToString(result, false);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException e) {
            throw Lang.wrapThrow(e);
        }
    }

    public static String decrypt(String result) {
        return decrypt(result, DEFAULT_KEY);
    }

    public static String decrypt(String result, String key) {
        return decrypt(Base64.decode(result), key);
    }

    public static String decrypt(byte[] result, String key) {
        return decrypt(result, loadKey(key));
    }

    public static String decrypt(String result, Key key) {
        return decrypt(Base64.decode(result), key);
    }

    public static String decrypt(byte[] result, Key key) {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // 初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.DECRYPT_MODE, key);
            result = cipher.doFinal(result);
            return new String(result, StandardCharsets.UTF_8);
        } catch (NoSuchAlgorithmException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) {
            throw Lang.wrapThrow(e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy