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

matrix.boot.common.encrypt.AES Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.encrypt;

import matrix.boot.common.exception.ServiceException;
import matrix.boot.common.utils.StringUtil;

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

/**
 * AES对称加密解密
 *
 * @author wangcheng
 */
public class AES {

    /**
     * 加密
     *
     * @param content  内容
     * @param password 密钥
     * @return 加密后的字符串
     */
    public static String encrypt(String content, String password) {
        return encrypt(content, password, Algorithm.ECB_PKCS5);
    }

    /**
     * 解密
     *
     * @param content  加密的内容
     * @param password 密码
     * @return 解密后的字符串
     */
    public static String decrypt(String content, String password) {
        return decrypt(content, password, Algorithm.ECB_PKCS5);
    }

    /**
     * 加密
     *
     * @param content   内容
     * @param password  密钥
     * @param algorithm 算法类型
     * @return 加密后的字符串
     */
    public static String encrypt(String content, String password, Algorithm algorithm) {
        try {
            Cipher cipher = getCipher(password, Cipher.ENCRYPT_MODE, algorithm);
            return Base64.encrypt(cipher.doFinal(StringUtil.stringToByte(content)));
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 解密
     *
     * @param content   加密的内容
     * @param password  密码
     * @param algorithm 算法类型
     * @return 解密后的字符串
     */
    public static String decrypt(String content, String password, Algorithm algorithm) {
        try {
            Cipher cipher = getCipher(password, Cipher.DECRYPT_MODE, algorithm);
            return new String(cipher.doFinal(Objects.requireNonNull(Base64.decrypt(content))));
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 获取加密解密器
     *
     * @param password  密钥
     * @param type      加密解密类型
     * @param algorithm 算法类型
     * @return 加密解密器
     */
    private static Cipher getCipher(String password, Integer type, Algorithm algorithm) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(StringUtil.stringToByte(MD5.get16(password)), "AES");
            Cipher cipher = Cipher.getInstance(algorithm.value);
            cipher.init(type, secretKeySpec);
            return cipher;
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 算法枚举
     */
    public enum Algorithm {

        ECB_NO("AES/ECB/NoPadding"),
        ECB_PKCS5("AES/ECB/PKCS5Padding"),
        CBC_NO("AES/CBC/NoPadding"),
        CBC_PKCS5("AES/CBC/PKCS5Padding"),
        CFB_NO("AES/CFB/NoPadding"),
        CFB_PKCS5("AES/CFB/PKCS5Padding");


        Algorithm(String value) {
            this.value = value;
        }

        /**
         * 值
         */
        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy