加密算法 : AES(Advanced Encryption Standard,高级加密标准)是一种对称加密算法,加密和解密使用相同的密钥。

参考:

Java实现AES加密算法

AES简介

高级加密标准(AES,Advanced Encryption Standard)为最常见的对称加密算法(微信小程序加密传输就是用这个加密算法的)。对称加密算法也就是加密和解密用相同的密钥,具体的加密流程如下图:

在这里插入图片描述

工具类

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class EncrypAES {
    /** 默认密钥,实际项目中可配置注入或数据库读取 */
    private static String defaultKey = "helloworld";

    /** 加密工具 */
    private Cipher encryptCipher = null;

    /** 解密工具 */
    private Cipher decryptCipher = null;

    private static EncrypAES instance = new EncrypAES(defaultKey);

    public static EncrypAES getInstance() {
        return instance;
    }

    public EncrypAES(String keyvalue) {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        if (keyvalue == null)
            keyvalue = defaultKey;
        byte[] arrBTmp = null;
        try {
            arrBTmp = keyvalue.getBytes("utf-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // 创建一个空的length位字节数组(默认值为0)
        byte[] arrB = new byte[16];

        // 将原始字节数组转换为8位
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        // 生成密钥
        Key key = new javax.crypto.spec.SecretKeySpec(arrB, "AES");

        // 生成Cipher对象,指定其支持的DES算法
        try {
            encryptCipher = Cipher.getInstance("AES");
            encryptCipher.init(Cipher.ENCRYPT_MODE, key);

            decryptCipher = Cipher.getInstance("AES");
            decryptCipher.init(Cipher.DECRYPT_MODE, key);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    /**
     * 对字符串加密
     *
     * @param str
     * @return
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public String encrytor(String str) throws InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {
        byte[] src = null;
        try {
            src = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return parseByte2HexStr(encryptCipher.doFinal(src));
    }

    /**
     * 对字符串解密
     *
     * @param buff
     * @return
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public String decryptor(String buff) throws InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {
        return new String(decryptCipher.doFinal(parseHexStr2Byte(buff)));
    }

    /**
     * @param args
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     * @throws InvalidKeyException
     */
    public static void main(String[] args) throws Exception {
        EncrypAES de1 = new EncrypAES("woshichuanqi");
        String msg = "9A5C1E41066458D50F91636A111FED89.1560843070936";
        String demsg = null;

        System.out.println("明文是:" + msg);
        long start = System.currentTimeMillis();
        demsg = de1.encrytor(msg);
        long end = System.currentTimeMillis();

        System.out.println("加密后:" + demsg);

        System.out.println("加密耗时:" + (end - start));

        start = System.currentTimeMillis();
        String value = de1.decryptor(demsg);
        end = System.currentTimeMillis();

        System.out.println("解密后:" + value);
        System.out.println("解密耗时:" + (end - start));
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
}

//明文是:9A5C1E41066458D50F91636A111FED89.1560843070936
//加密后:5E4D047CC156FABE4BC6C1781440AF1FEC33B8D0876EF36318BD94A4F83B874D34DC98F54F9EAB3AB9A107747D83932D
//加密耗时:1
//解密后:9A5C1E41066458D50F91636A111FED89.1560843070936
//解密耗时:0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值