天天看點

一個簡單的aes加密工具類(java向)

直接上代碼.

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;

public class AesEncrypt {

    private final static String ORIGIN_KEY = "12jgUQFppppuwkkj";
    private final static SecretKey SECRET_KEY = initSecretKey();

    private static SecretKey initSecretKey() {
        SecretKeySpec aes = new SecretKeySpec(ORIGINKEY.getBytes(), "AES");
        return aes;
    }
    
    /**
     * 加密
     * @param source 一般字元串
   	 * @return base64Str base64字元串
     * @throws Exception
     */
    public static String encryptAES(String source) throws Exception {
        if (source==null){
            return null;
        }
        byte[] bytes1 = base642Byte(Base64Encode.encode(source));
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, SECRETKEY);
        byte[] bytes2 = cipher.doFinal(bytes1);
        return byte2Base64(bytes2);
    }

    /**
     * 解密
     * @param base64Code 一般字元串base64
     * @return base64Str
     * @throws Exception
     */
    public static String decryptAES(String base64Code) throws Exception {
        if (base64Code==null){
            return null;
        }
        byte[] bytes1 = base642Byte(base64Code);
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, SECRETKEY);
        byte[] bytes2 = cipher.doFinal(bytes1);
        return Base64Encode.decode(byte2Base64(bytes2));
    }

    /**
     * 位元組數組轉Base64編碼
     */
    public static String byte2Base64(byte[] bytes) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }

    /**
     * Base64編碼轉位元組數組
     */
    public static byte[] base642Byte(String base64Key) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(base64Key);
    }
}
           

下面是配合使用的base64工具類,可以完成一般字元串和base64字元串之間的轉換。

下面這個類是我從csdn擷取的,是以沒有做代碼合并。

這個類使用的base64依賴包(org.apache.commons.codec.binary.Base64),和我的aes加密工具類使用的不同,更推薦這個包

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;

public class Base64Encode {

        private static final String charset = "utf-8";

        /**
         * 解碼成一般字元串
         */
        public static String decode(String data) {
            try {
                if (null == data) {
                    return null;
                }
                return new String(Base64.decodeBase64(data.getBytes(charset)), charset);
            } catch (UnsupportedEncodingException e) {
                System.err.println(e);
            }
            return null;
        }

        /**
         * 編碼成base64字元串
         */
        public static String encode(String data) {
            try {
                if (null == data) {
                    return null;
                }
                return new String(Base64.encodeBase64(data.getBytes(charset)), charset);
            } catch (UnsupportedEncodingException e) {
                System.err.println(e);
            }
            return null;
        }
}
           

需要注意的幾點:

1,AesEncrypt類中的ORIGIN_KEY是用于生成128位AES秘鑰的源字元串,長度必須是16(128/8)個字元,是解密的關鍵。用于生産環境時,這個字元串需要用戶端和伺服器約定好,且一定要對這個常量做安全處理。

2,加密後傳回的是base64字元串,解密前不要試圖把密文轉成一般字元串,否則再回溯成byte[]數組時會有位數丢失導緻解密失敗

3,使用這個加解密邏輯時,配合使用時間戳加密驗證字段,可以提高通話驗證的可操作性

4,這個工具類應對一般加解密需求足夠了,若有更高的安全需求,推薦一篇講aes+rsa雙向加密的分享帖: https://blog.csdn.net/qq_32523587/article/details/79146977