您的位置 首页 java

AES加密解密工具类及使用

AES介绍

AES即高级加密标准(Advanced Encryption Standard)的缩写,是一种区块加密标准。

AES算法比DES算法更加安全,DES使用56位密钥,比较容易被破解,而AES可以使用128、192、和256位密钥,并且分组加密和解密数据,更加安全可靠。

AES工具类源码

 public class AesUtils {
    private static final String CHAR_ENCODING = "UTF-8";
    private static final String ECB_AES_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String CBC_AES_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String AES = "AES";
    public static final int IV_LEN = 16;

    /**
     * 加密
     */    public static String encrypt(String data, String key) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(CHAR_ENCODING), AES);
            Cipher cipher = Cipher.getInstance(ECB_AES_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encrypted = cipher.doFinal(data.getBytes(CHAR_ENCODING));
            return getBase64Str(encrypted);
        } catch (Exception e) {
            throw new RuntimeException("encrypt fail!", e);
        }
    }

    /**
     * CBC模式加密
     */    public static String encryptCBC(String data, String key) {
        try {
            SecretKeySpec secKey = new SecretKeySpec(key.getBytes(CHAR_ENCODING), AES);
            Cipher cipher = Cipher.getInstance(CBC_AES_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secKey, getIvByKey(key));
            byte[] encrypted = cipher.doFinal(data.getBytes(CHAR_ENCODING));
            return getBase64Str(encrypted);
        } catch (Exception e) {
            throw new RuntimeException("encryptCBC fail!", e);
        }
    }

    /**
     * 解密
     */    public static String decrypt(String data, String key) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(CHAR_ENCODING), AES);
            Cipher cipher = Cipher.getInstance(ECB_AES_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] result = cipher.doFinal(Base64.getDecoder().decode(data));
            return new String(result, CHAR_ENCODING);
        } catch (Exception e) {
            throw new RuntimeException("decrypt fail!", e);
        }
    }

    /**
     * CBC模式解密
     */    public static String decryptCBC(String data, String key) {
        try {
            SecretKeySpec secKey = new SecretKeySpec(key.getBytes(CHAR_ENCODING), AES);
            Cipher cipher = Cipher.getInstance(CBC_AES_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secKey, getIvByKey(key));
            byte[] original = cipher.doFinal(Base64.getDecoder().decode(data));
            return new String(original, CHAR_ENCODING);
        } catch (Exception e) {
            throw new RuntimeException("decryptCBC fail!", e);
        }
    }

    private static IvParameterSpec getIvByKey(String key) throws UnsupportedEncodingException {
        byte[] iv = new byte[IV_LEN];
        System.arraycopy(key.getBytes(CHAR_ENCODING), 0, iv, 0, IV_LEN);
        return new IvParameterSpec(iv);
    }

    public static String getBase64Str(byte[] encrypted) throws UnsupportedEncodingException {
        byte[] enc64 = Base64.getEncoder().encode(encrypted);
        return new String(enc64, CHAR_ENCODING);
    }
}  

工具类的使用

 public static final String secret32 = "YH9H41tA9StPDSklnk9ioe7aj80DugOj";
public static final String SOURCE = "sdljflksdj";

@Test
public void cbc() throws Exception {
   //16,24,32长度都能成功加解密
   String secret = secret32.substring(0,16);
   String hello_world = AesUtils.encryptCBC(SOURCE, secret);
   String decrypt = AesUtils.decryptCBC(hello_world, secret);
}  

 @Test
public void testAesUtils() throws Exception {
   //16,24,32长度都能成功加解密
   String secret = secret32.substring(0,24);
   String hello_world = AesUtils.encrypt(SOURCE, secret);
   String decrypt = AesUtils.decrypt(hello_world, secret);
}  

文章来源:智云一二三科技

文章标题:AES加密解密工具类及使用

文章地址:https://www.zhihuclub.com/172070.shtml

关于作者: 智云科技

热门文章

网站地图