aes加密AES/ECB/PKCS7Padding||AES/ECB/PKCS5Padding

AesUtils

加密方式:ECB、CBC、CFB、OFB、CTR

填充方式:PKCS5Padding、PKCS7Padding、zeropadding、iso10126、ansix923

数据块长度:128位、192位、256位

密钥长度:16位、24位、32位


    /**
     * 编码
     */
    public static String CHARSET="UTF-8";
    public static String ALGORITHM = "AES";
    /**
     * 加密方式:ECB、CBC、CFB、OFB、CTR
     * 填充方式:PKCS5Padding、PKCS7Padding、zeropadding、iso10126、ansix923
     * 数据块长度:128位、192位、256位
     */
    @AllArgsConstructor
    @Getter
    public enum AlgorithmECBEnum{
     
        ALGORITHM_ECB_5("AES/ECB/PKCS5Padding"),
        ALGORITHM_ECB_7("AES/ECB/PKCS7Padding"),
        ALGORITHM_ECB_z("AES/ECB/zeroPadding"),
        ALGORITHM_ECB_i("AES/ECB/iso10126"),
        ALGORITHM_ECB_a("AES/ECB/ansix923")
        ;
        private String algorithm;
    }
    @AllArgsConstructor
    @Getter
    public enum AlgorithmCBCEnum{
     
        ALGORITHM_CBC_5("AES/CBC/PKCS5Padding"),
        ALGORITHM_CBC_7("AES/CBC/PKCS7Padding"),
        ALGORITHM_CBC_z("AES/CBC/zeroPadding"),
        ALGORITHM_CBC_i("AES/CBC/iso10126"),
        ALGORITHM_CBC_a("AES/CBC/ansix923"),
        ;
        private String algorithm;
    }
    @AllArgsConstructor
    @Getter
    public enum AlgorithmCFBEnum{
     
        ALGORITHM_CFB_5("AES/CFB/PKCS5Padding"),
        ALGORITHM_CFB_7("AES/CFB/PKCS7Padding"),
        ALGORITHM_CFB_z("AES/CFB/zeroPadding"),
        ALGORITHM_CFB_i("AES/CFB/iso10126"),
        ALGORITHM_CFB_a("AES/CFB/ansix923")
        ;
        private String algorithm;
    }
    @AllArgsConstructor
    @Getter
    public enum AlgorithmOFBEnum{
     
        ALGORITHM_OFB_5("AES/OFB/PKCS5Padding"),
        ALGORITHM_OFB_7("AES/OFB/PKCS7Padding"),
        ALGORITHM_OFB_z("AES/OFB/zeroPadding"),
        ALGORITHM_OFB_i("AES/OFB/iso10126"),
        ALGORITHM_OFB_a("AES/OFB/ansix923")
        ;
        private String algorithm;
    }
    @AllArgsConstructor
    @Getter
    public enum AlgorithmCTREnum{
     
        ALGORITHM_CTR_5("AES/CTR/PKCS5Padding"),
        ALGORITHM_CTR_7("AES/CTR/PKCS7Padding"),
        ALGORITHM_CTR_z("AES/CTR/zeroPadding"),
        ALGORITHM_CTR_i("AES/CTR/iso10126"),
        ALGORITHM_CTR_a("AES/CTR/ansix923")
        ;
        private String algorithm;
    }

    /**
     * key的长度
     * 16=128 bit
     * 24=192 bit
     * 32=256 bit
     */
    @AllArgsConstructor
    @Getter
    public enum BitEnum{
     
        ALGORITHM_128(16),
        ALGORITHM_192(24),
        ALGORITHM_256(32);
        private Integer bit;

        public static boolean contains(Integer bit){
     
            if (null == bit){
     
                return false;
            }
            for (BitEnum bitEnum : BitEnum.values()){
     
                if (bitEnum.getBit().equals(bit)){
     
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * PKCS7Padding 需要用到
     */
    public static boolean initialized = false;
    public static void initialize(){
     
        if (initialized) return;
        Security.addProvider(new BouncyCastleProvider());
        initialized = true;
    }

    /**
     * encode ecb
     * @param key 密钥
     * @param content 明文
     * @param algorithmEnum 加密方式/补码方式
     * @return
     */
    public static String encodeECB(String key, String content, AlgorithmECBEnum algorithmEnum){
     
        Assert.notNull(content,"加密内容不能为空");
        Assert.notNull(key,"加密的key不能为空");
        Assert.notNull(algorithmEnum,"加密方式/补码方式不能为空");
        int length = key.length();
        if (!BitEnum.contains(length)){
     
            throw new RuntimeException("key的长度必须是16位或者24位或者32位");
        }
        try{
     
            Cipher cipher = Cipher.getInstance(algorithmEnum.getAlgorithm());
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET),ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE,keySpec);
            byte[] bytes = cipher.doFinal(content.getBytes(CHARSET));
            return Base64Util.encodeBase64(bytes);
        }catch (Exception e){
     
            throw new RuntimeException(e);
        }
    }

    /**
     * encode cbc
     * @param key 密钥
     * @param content 明文
     * @param algorithmEnum 加密方式/补码方式
     * @param iv 使用CBC模式,需要一个向量iv,可增加加密算法的强度
     * @return
     */
    public static String encodeCBC(String key, String content, AlgorithmCBCEnum algorithmEnum,String iv){
     
        Assert.notNull(content,"加密内容不能为空");
        Assert.notNull(key,"加密的key不能为空");
        Assert.notNull(iv,"向量iv不能为空");
        Assert.notNull(algorithmEnum,"加密方式/补码方式不能为空");
        int length = key.length();
        if (!BitEnum.contains(length)){
     
            throw new RuntimeException("key的长度必须是16位或者24位或者32位");
        }
        try{
     
            Cipher cipher = Cipher.getInstance(algorithmEnum.getAlgorithm());
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET),ALGORITHM);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(CHARSET));
            cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivParameterSpec);
            byte[] bytes = cipher.doFinal(content.getBytes(CHARSET));
            return Base64Util.encodeBase64(bytes);
        }catch (Exception e){
     
            throw new RuntimeException(e);
        }
    }

你可能感兴趣的:(工作笔记)