AES加密和解密,key需要32位

AES加密和解密,key需要32位

package com.example.core.mydemo.sign;

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.SecretKeySpec;


/**
 * 加密后字符串为str:XDUWkZ088cmXUGohYxGZ3g==
 * 解密后字符串为str2:hello world
 */
public class AesEncryptTestUtils {

    private static Logger logger = LoggerFactory.getLogger(AesEncryptTestUtils.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){
            logger.error("AES加密失败,srcContent为{}",srcContent);
            throw new RuntimeException("AES加密失败");
        }
    }

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


    /**
     * 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);
    }

    public static void main(String[] args) throws Exception{
        try {
            //key需要32位
            String key = "1234567890tkltktqVdTstvuhlZHTest";
            String str = aesEncryptContent("hello world", key);
            System.out.println("加密后字符串为str:"+str);

            String str2 = aesDecryptContent(str, key);
            System.out.println("解密后字符串为str2:"+str2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

posted on 2023-06-30 18:50  oktokeep  阅读(683)  评论(0编辑  收藏  举报