一个 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();
        }
    }
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值