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

com.hecloud.runtime.common.encryptor.AESEncrptor Maven / Gradle / Ivy

package com.hecloud.runtime.common.encryptor;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

/**
 * AES加密实现类
 *
 * @author LoveInBJ
 */
public class AESEncrptor implements Encryptor {
    private static Logger logger = LoggerFactory.getLogger(AESEncrptor.class);
    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String KEY_ALGORITHM = "AES";
    private static final String CHARSET = "utf-8";
    private static String DEFAULT_KEY = "SG90ZWFtTG92ZUluQkohIQ==";
    private static String DEFAULT_IV = "SG90ZWFtTG92ZUluQkohIQ==";

    static {
        try {
            DEFAULT_IV = new String(Base64Coder.decode(DEFAULT_IV.toCharArray()));
            DEFAULT_KEY = new String(Base64Coder.decode(DEFAULT_KEY.toCharArray()));
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    /**
     * 获取key和iv
     *
     * @param context  上下文
     * @param mode     模式
     * @param password 密码
     * @return IV二进制数组
     * @throws Exception 运行异常 异常
     */
    public static byte[] createKeyAndIv(byte[] context, int mode, String password) throws Exception {
        if (StringUtils.isEmpty(password)) {
            password = DEFAULT_KEY;
        } else {
            if (password.length() % 16 != 0) {
                throw new Exception("Invalid key length!");
            }
        }

        byte[] key = password.getBytes(CHARSET);
        byte[] iv = DEFAULT_IV.getBytes(CHARSET);
        return cipherFilter(context, mode, key, iv);
    }

    /**
     * 执行操作
     *
     * @param context 上下文
     * @param mode    模式
     * @param key     key
     * @param iv      iv
     * @return dd
     * @throws Exception 运行异常 异常
     */
    public static byte[] cipherFilter(byte[] context, int mode, byte[] key, byte[] iv) throws Exception {
        Key secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
        AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(mode, secretKeySpec, ivParameterSpec);
        return cipher.doFinal(context);
    }

    /**
     * AES加密
     *
     * @param content  待加密数据
     * @param password 加密密码
     * @return Base64转码后的加密数据
     * @throws Exception 运行异常 异常
     */
    @Override
    public String encrypt(String content, String password) throws Exception {
        try {
            byte[] decode = content.getBytes(CHARSET);
            return Base64.encodeBase64String(encrypt(decode, password));
        } catch (Exception e) {
            logger.error("encryptor Exception!", e);
        }
        return null;
    }

    /**
     * AES加密
     *
     * @param content  明文
     * @param password 密码
     * @return 密文
     */
    @Override
    public byte[] encrypt(byte[] content, String password) {
        try {
            return createKeyAndIv(content, Cipher.ENCRYPT_MODE, password);
        } catch (Exception e) {
            logger.error("encryptor Exception!", e);
        }
        return null;
    }

    /**
     * AES解密
     *
     * @param content  密文
     * @param password 密码
     * @return Base64解码后的明文
     * @throws Exception 运行异常 异常
     */
    @Override
    public String decrypt(String content, String password) throws Exception {
        try {
            byte[] decode = Base64.decodeBase64(content);
            byte[] result = decrypt(decode, password);
            return new String(result, CHARSET);
        } catch (Exception e) {
            logger.error("decrypt exception!", e);
        }
        return null;
    }

    /**
     * AES解密
     *
     * @param content  密文
     * @param password 密码
     * @return Base64解码后的明文
     */
    @Override
    public byte[] decrypt(byte[] content, String password) {
        try {
            return createKeyAndIv(content, Cipher.DECRYPT_MODE, password);
        } catch (Exception e) {
            logger.error("decrypt exception!", e);
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        AESEncrptor encrptor = new AESEncrptor();
        String data = args[0];
        String keys = args[1];
        if (StringUtils.isEmpty(data)) {
            System.out.println("data should not be null!");
        } else {
            if (StringUtils.isEmpty(keys)) {
                keys = "HoteamLoveInBJ!!";
            } else if (keys.length() < 16) {
                System.out.println("password length should be more than 16 characters !");
                System.exit(0);
            }
            data = encrptor.encrypt(data, keys);
            System.out.println(data);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy