使用java實現AES算法的加解密(親測可用)

話不多說,直接上代碼

 

import javax.crypto.Cipher;  
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;  
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
 
public class AESUtil {  
    
    private static String key="111";
    
    
    /**
     * 加密
     * @param content
     * @param strKey
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(String content,String strKey ) throws Exception {
        SecretKeySpec skeySpec = getKey(strKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(content.getBytes());
        return  encrypted;
    }

    /**
     * 解密
     * @param strKey
     * @param content
     * @return
     * @throws Exception
     */
    public static String decrypt(byte[] content,String strKey ) throws Exception {
        SecretKeySpec skeySpec = getKey(strKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] original = cipher.doFinal(content);
        String originalString = new String(original);
        return originalString;
    }

    private static SecretKeySpec getKey(String strKey) throws Exception {
        byte[] arrBTmp = strKey.getBytes();
        byte[] arrB = new byte[16]; // 創建一個空的16位字節數組(默認值爲0)

        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }

        SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");

        return skeySpec;
    }
    
    
    /**
     * base 64 encode
     * @param bytes 待編碼的byte[]
     * @return 編碼後的base 64 code
     */
    public static String base64Encode(byte[] bytes){
        return new BASE64Encoder().encode(bytes);
    }

    /**
     * base 64 decode
     * @param base64Code 待解碼的base 64 code
     * @return 解碼後的byte[]
     * @throws Exception
     */
    public static byte[] base64Decode(String base64Code) throws Exception{
        return base64Code.isEmpty() ? null : new BASE64Decoder().decodeBuffer(base64Code);
    }
    
    /**
     * AES加密爲base 64 code
     * @param content 待加密的內容
     * @param encryptKey 加密密鑰
     * @return 加密後的base 64 code
     * @throws Exception  //加密傳String類型,返回String類型
     */
    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(encrypt(content, encryptKey));
       
    }
    /**
     * 將base 64 code AES解密
     * @param encryptStr 待解密的base 64 code
     * @param decryptKey 解密密鑰
     * @return 解密後的string   //解密傳String類型,返回String類型
     * @throws Exception
     */
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return encryptStr.isEmpty() ? null : decrypt(base64Decode(encryptStr), decryptKey);
    }
    
    
    
    public static void main(String[] args) throws Exception {
//        String encrypt = aesEncrypt("root", key);
//        System.out.println(encrypt);
        String decrypt = aesDecrypt("+hf8qB4LhS7L7BzBy83bLg==", key);
        System.out.println(decrypt);
    }

 

如果遇到了BASE64Encoder和BASE64Decoder報錯的話,請參考 https://www.cnblogs.com/liuyangfirst/p/6429913.html

 

 

 

 

發佈了41 篇原創文章 · 獲贊 15 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章