Java对称加解密算法AES

2024-10-09 11:02:26 浏览数 (1)

Java对称加解密算法AES

代码语言:javascript复制
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;

/**
 * 敏感加密入参出参字段工具类
 */
public class AesEncryptUtils {

    private static Logger logger = LoggerFactory.getLogger(AesEncryptUtils.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);
    }

    /**
     * 手机号加密后字符串为str:OgB897 iuJYjuw4H1Ou3xw==
     * 手机号解密后字符串为str2:19966667777
     * 姓名加密后字符串为str:rfGUZPPHuG91j/cEjJJbOA==
     * 姓名解密后字符串为str2:张三
     * 身份证号加密后字符串为str:UCK1di7NzzjjjNAGZMg2iS/c6jnEv0fips1zo7ww4RQ=
     * 身份证号解密后字符串为str2:463300122545556699
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{
        try {
            //注意:密钥需要32位
            String key = "testkey111testkey111testkey11132";
            String str = aesEncryptContent("19966667777", key);
            System.out.println("手机号加密后字符串为str:" str);
            String str2 = aesDecryptContent(str, key);
            System.out.println("手机号解密后字符串为str2:" str2);

            str = aesEncryptContent("张三", key);
            System.out.println("姓名加密后字符串为str:" str);
            str2 = aesDecryptContent(str, key);
            System.out.println("姓名解密后字符串为str2:" str2);

            str = aesEncryptContent("463300122545556699", key);
            System.out.println("身份证号加密后字符串为str:" str);
            str2 = aesDecryptContent(str, key);
            System.out.println("身份证号解密后字符串为str2:" str2);

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

}

0 人点赞