AES加密解密工具类及使用

AES加密解密工具类及使用AES介绍AES即高级加密标准(Advanced Encryption Standard)的缩写,是一种区块加密标准。AES算法比DES算法更加

大家好,欢迎来到IT知识分享网。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);
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/62928.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信