aes 암호화 AES/ECB/PKCS7Padding|||AES/ECB/PKCS5Padding

25775 단어 작업 노트
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);
        }
    }

좋은 웹페이지 즐겨찾기