AES对称加密算法

AES对称加密算法

import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AesUtils {

    private static Logger logger = LoggerFactory.getLogger(AesUtils.class);
    private static final String AES_ALG = "AES";
    /**
     * AES算法
     */
    private static final String AES_CBC_PCK_ALG = "AES/ECB/PKCS5Padding";
    
    public static final String CHARSET_UTF8 = "UTF-8";

    /**
     * AES加密
     *
     * @param srcContent
     * @return
     * @throws Exception
     */
    public static String aesEncryptContent(String srcContent,String AES_KEY) {
        try{
            //如果为空不加密
            if(StringUtils.isEmpty(srcContent)){
                return srcContent;
            }
            String aesStr =  aesEncrypt(srcContent,AES_KEY,CHARSET_UTF8);
            return aesStr;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("AES加密失败,srcContent为{}",srcContent);
            throw new RuntimeException("AES加密失败");
        }
    }

    /**
     * AES解密
     *
     * @param aesContent
     * @return
     * @throws Exception
     */
    public static String aesDecryptContent(String aesContent,String AES_KEY){
        try{
            //如果为空不加密
            if(StringUtils.isEmpty(aesContent)){
                return aesContent;
            }
            String srcStr = aesDecrypt(aesContent,AES_KEY,CHARSET_UTF8);
            return srcStr;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("AES解密失败,aesContent为{}",aesContent);
            throw new RuntimeException("AES解密失败");
        }

    }

    /**
     * AES加密
     *
     * @param content
     * @param aesKey
     * @param charset
     * @return
     */
    private static String aesEncrypt(String content, String aesKey, String charset)
            throws Exception {
            Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);
            cipher.init(Cipher.ENCRYPT_MODE,
                    new SecretKeySpec(aesKey.getBytes(), AES_ALG));

            byte[] encryptBytes = cipher.doFinal(content.getBytes(charset));
            return new String(Base64.encodeBase64(encryptBytes));
    }

    /**
     * AES解密
     *
     * @param content
     * @param key
     * @param charset
     * @return
     */
    private static String aesDecrypt(String content, String key, String charset)
            throws Exception {
        Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(),
                AES_ALG));

        byte[] cleanBytes = cipher.doFinal(Base64.decodeBase64(content.getBytes()));
        return new String(cleanBytes, charset);
    }

    /**
     * 输出:
     * 加密后字符串为:6Lmstq5QDY7n06cU/ygZYg==
     * 解密后字符串为:测试123
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{
        try {
            //32位,否则报错:java.security.InvalidKeyException: Invalid AES key length: 27 bytes
            String myAESKey = "testkey123testkey123testkey12345";
            String srcstr = "测试123";
            String enc = aesEncryptContent(srcstr,myAESKey);
            System.out.println("加密后字符串为:"+enc);
            String src = aesDecryptContent(enc,myAESKey);
            System.out.println("解密后字符串为:"+src);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

posted on 2022-10-08 17:05  oktokeep  阅读(163)  评论(0编辑  收藏  举报