一个 AES 加解密工具类

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.Charset;
import java.security.SecureRandom;

public class AESUtil {
    //用户密钥
    private static byte[] keyValue = new byte[] {22, 25, -35, -45, 25, 98, -55, -45, 10, 35, -45, 25, 26, -95, 25,
            -65, -78, -99, 85, 45, -62, 10, -0, 11, -35, 48, -98, 65, -32, 14, -78, 25, 36, -56, -45, -45, 12, 15,
            -35, -75, 15, -14, 62, -25, 33, -45, 55, 68, -88};
    private static Charset charset = Charset.forName("UTF-8");
    private static String cipher = "AES";
    private static SecretKey secretKey;

    static {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance(cipher);
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(keyValue);

            keyGenerator.init(128, secureRandom);
            secretKey = keyGenerator.generateKey();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加密
     */
    public static String encrypt(String msg) throws Exception {
        Cipher c = Cipher.getInstance(cipher);
        c.init(Cipher.ENCRYPT_MODE, secretKey);
        //加密并转换成16进制字符串
        return asHex(c.doFinal(msg.getBytes(charset)));
    }

    /**
     * 解密
     */
    public static String decrypt(String value) throws Exception {
        Cipher c = Cipher.getInstance(cipher);
        c.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(c.doFinal(asBin(value)), charset);
    }

    /**
     * 将字节数组转换成16进制字符串
     */
    private static String asHex(byte buf[]) {
        StringBuffer sb = new StringBuffer(buf.length * 2);
        int i;
        for (i = 0; i < buf.length; i++) {
            if (((int) buf[i] & 0xff) < 0x10)//小于十前面补零  
                sb.append("0");
            sb.append(Long.toString((int) buf[i] & 0xff, 16));
        }
        return sb.toString();
    }

    /**
     * 将16进制字符串转换成字节数组
     */
    private static byte[] asBin(String src) {
        if (src.length() < 1) return null;
        byte[] encrypted = new byte[src.length() / 2];
        for (int i = 0; i < src.length() / 2; i++) {
            int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);//取高位字节  
            int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);//取低位字节  
            encrypted[i] = (byte) (high * 16 + low);
        }
        return encrypted;
    }

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 1000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String enc = encrypt("aaa1");
                        System.out.println("解密:" + enc);
                        String decrypt = decrypt(enc);
                        System.out.println("解密:" + decrypt);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}  

你可能感兴趣的:(一个 AES 加解密工具类)