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

com.soento.core.util.AesUtil Maven / Gradle / Ivy

package com.soento.core.util;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Security;

/**
 * AES 是一种可逆加密算法,对用户的敏感信息加密处理,
 * 对原始数据进行AES加密后,在进行Base64编码转化;
 *
 * @author soento
 **/
public final class AesUtil {

    public static String encryptByCbcPkcs5(String input, String key, Charset charset, String iv) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] raw = key.getBytes(charset);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            //使用CBC模式,需要一个向量iv,可增加加密算法的强度
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(charset));
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
            byte[] encrypted = cipher.doFinal(input.getBytes(charset));
            //此处使用BASE64做转码。
            return Base64Util.encrypt(encrypted, charset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String encryptByCbcPkcs5(String input, String key, String iv) {
        return encryptByCbcPkcs5(input, key, StandardCharsets.UTF_8, iv);
    }

    public static String decryptByCbcPkcs5(String input, String key, Charset charset, String iv) {
        try {
            byte[] raw = key.getBytes(charset);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(charset));
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
            //先用base64解密
            byte[] encrypted1 = Base64.decodeBase64(input.getBytes(charset));
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, charset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String decryptByCbcPkcs5(String input, String key, String iv) {
        return decryptByCbcPkcs5(input, key, StandardCharsets.UTF_8, iv);
    }

    public static String encryptByEcbPkcs7(String content, String key, Charset charset) {
        try {
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            byte[] realKey = getSecretKey(key, charset);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(realKey, "AES"));
            byte[] data = cipher.doFinal(content.getBytes(charset));
            return Base64Util.encrypt(data, charset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String encryptByEcbPkcs7(String content, String key) {
        return encryptByEcbPkcs7(content, key, StandardCharsets.UTF_8);
    }

    public static String decryptByEcbPkcs7(String content, String key, Charset charset) {
        try {
            Security.addProvider(new BouncyCastleProvider());
            byte[] decodeBytes = Base64.decodeBase64(content);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            byte[] realKey = getSecretKey(key, charset);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(realKey, "AES"));
            byte[] realBytes = cipher.doFinal(decodeBytes);
            return new String(realBytes, charset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String decryptByEcbPkcs7(String content, String key) {
        return decryptByEcbPkcs7(content, key, StandardCharsets.UTF_8);
    }

    private static byte[] getSecretKey(String key, Charset charset) {
        try {
            final byte paddingChar = ' ';
            byte[] realKey = new byte[32];
            byte[] byteKey = key.getBytes(charset);
            for (int i = 0; i < realKey.length; i++) {
                if (i < byteKey.length) {
                    realKey[i] = byteKey[i];
                } else {
                    realKey[i] = paddingChar;
                }
            }
            return realKey;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy