AES加密工具

 static class AESUtils {

        // 加密后是Hex格式
        public static String encoding(byte[] data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
            final Key datakey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, datakey);
            byte[] encryptData = cipher.doFinal(data);
            String result = parseByte2HexStr(encryptData);
            System.out.println("AES 加密后 " + result);
            return result;
        }

        // Hex格式解密
        public static String decoding(byte[] data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
            byte[] bytes = parseHexStr2Byte(data);
            final Key datakey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, datakey);
            byte[] encryptData = cipher.doFinal(bytes);
            String result = new String(encryptData);
            System.out.println("AES 解密后 " + result);
            return result;
        }

        /**
         * 将二进制转换成16进制
         *
         * @param buf
         * @return
         */
        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.toLowerCase());
            }
            return sb.toString();
        }

        /**
         * 16 进制转 二进制
         *
         * @param buf
         * @return
         */
        public static byte[] parseHexStr2Byte(byte[] buf) {
            String hexStr = new String(buf);
            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;
        }

        // Base64格式
        public static String encodingBase64(byte[] data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, InstantiationException, IllegalAccessException {
            final Key datakey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, datakey);
            byte[] encryptData = cipher.doFinal(data);
            String result = parseByte2Base64Str(encryptData);
            System.out.println("AES 加密后 " + result);
            return result;
        }

        // Base64格式解密
        public static String decodingBase64(byte[] data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, InstantiationException, IllegalAccessException {
            data = parseBase642Byte(data);
            final Key datakey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, datakey);
            byte[] encryptData = cipher.doFinal(data);
            String result = new String(encryptData,"UTF-8").trim();
            System.out.println("AES 解密后 " + result);
            return result;
        }

        public static String parseByte2Base64Str(byte[] bytes) throws InstantiationException, IllegalAccessException {
            String encode = BASE64Encoder.class.newInstance().encode(bytes);
            return encode;
        }

        public static byte[] parseBase642Byte(byte[] base64) throws InstantiationException, IllegalAccessException, IOException {
            BASE64Decoder base64Decoder = BASE64Decoder.class.newInstance();
            byte[] bytes = base64Decoder.decodeBuffer(new String(base64));
            return bytes;
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值