AES 복호화 도구 클래스 AESUtil

3361 단어 Java
pom. xml 먼저 의존 도입:

    org.bouncycastle
    bcprov-jdk15on
    1.60

import org.apache.commons.net.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @Auther: 
 * @Date: 2018/10/19 15:49
 * @Description:
 */
public class AESUtil {
    static Logger logger = LoggerFactory.getLogger(AESUtil.class);
    //   
    public static String key = "AD42F6697B035B7580E4FEF93BE20BAD";
    private static String charset = "utf-8";
    //    
    private static int offset = 16;
    //      :     AES,     CBC,     PKCS5Padding
    private static String transformation = "AES/CBC/PKCS5Padding";
    //     :      AES   
    private static String algorithm = "AES";

    /**
     *   
     *
     * @param content
     * @return
     */
    public static String encrypt(String content) {
        return encrypt(content, key);
    }

    /**
     *   
     *
     * @param content
     * @return
     */
    public static String decrypt(String content) {
        return decrypt(content, key);
    }

    /**
     *   
     *
     * @param content        
     * @param key         
     * @return
     */
    public static String encrypt(String content, String key) {
        try {
            //    
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            //      iv         (         128 ),  AES     ,    iv             
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            //  AES   
            Cipher cipher = Cipher.getInstance(transformation);
            byte[] byteContent = content.getBytes(charset);
            //          
            cipher.init(Cipher.ENCRYPT_MODE, skey, iv);
            //   
            byte[] result = cipher.doFinal(byteContent);
            //  BASE64              
            return new Base64().encodeToString(result);
        } catch (Exception e) {
            logger.info("", e);
        }
        return null;
    }

    /**
     * AES(256)  
     *
     * @param content      
     * @param key         
     * @return     
     * @throws Exception
     */
    public static String decrypt(String content, String key) {
        try {

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            Cipher cipher = Cipher.getInstance(transformation);
            //             
            cipher.init(Cipher.DECRYPT_MODE, skey, iv);//    
            byte[] result = cipher.doFinal(new Base64().decode(content));
            return new String(result); //   
        } catch (Exception e) {
            logger.info("", e);
        }
        return null;
    }

    public static void main(String[] args) {
        String s = "hello World!123.   ";
        String encryptResultStr = encrypt(s);
        //   
        System.out.println("   :" + s);
        System.out.println("   :" + encryptResultStr);
        //   
        System.out.println("   :" + decrypt(encryptResultStr));
    }
}

좋은 웹페이지 즐겨찾기