AES加解密方式进行加密和解密

public class CryptionAESUtil{


        // 申明加密标准
        private final static String AES = "AES/CBC/PKCS5Padding";

        private static final String INSTANCE_NAME = "SHA1PRNG";

        private static final String ENCODE = "UTF-8";

        // 该矢量必须要8字节,可由编程人员自己设置
        private static byte[] EncryptionIV = "12345678abcdefgh".getBytes();

        private static String pwd=PathUtil.getValue("pwd");


        public CryptionAESUtil(){
                try{
                        EncryptionIV =PathUtil.getValue("miyao").getBytes(ENCODE);
                }catch (Exception e) {
                        e.printStackTrace();
                }
        }

        /**
        * 加密一个字节数组
        * 
        * @param 原数组
        *            byte[]
        * @throws Exception
        * @return 加密后的数组 byte[]
        */
        public byte[] EncryptionByteData(byte[] SourceData) throws Exception{
                    byte[] retByte = null;
                    try {
     
                         Cipher cipher = Cipher.getInstance(AES);
 
                         KeyGenerator keyGen = KeyGenerator.getInstance("AES");
                         //防止linux下 随机生成key
                         SecureRandom secureRandom = SecureRandom.getInstance(INSTANCE_NAME);  
                         secureRandom.setSeed(EncryptionIV);
                         keyGen.init(128, secureRandom);
                         SecretKey secretKey = keyGen.generateKey();
 
 
                        //  byte[] encode_secretKey = parseHexStr2Byte(pwd);
                        //  SecretKeySpec secretKey = new SecretKeySpec(encode_secretKey, "AES");
 
                        IvParameterSpec iv = new IvParameterSpec(EncryptionIV);
                        cipher.init(Cipher.ENCRYPT_MODE, secretKey , iv);
                        retByte = cipher.doFinal(SourceData);

                } catch (NoSuchAlgorithmException e) {  

                        e.printStackTrace();  

                } catch (NoSuchPaddingException e) {  

                        e.printStackTrace();  
                } catch (InvalidKeyException e) {  
                        e.printStackTrace();  
                }catch (IllegalBlockSizeException e) {  
                        e.printStackTrace();  
                } catch (BadPaddingException e) {  
                        e.printStackTrace();  
                }  
               return retByte;
        }


        /**
        * 解密一个字节数组
        * 
        * @param 原数据
        *            byte[]
        * @throws Exception
        * @return byte[]
        */
        public byte[] DecryptionByteData(byte[] SourceData) throws Exception{
                byte[] retByte = null;
                try {  
                        Cipher cipher = Cipher.getInstance(AES);

                        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
                        SecureRandom secureRandom = SecureRandom.getInstance(INSTANCE_NAME,"SUN");  
                        secureRandom.setSeed(EncryptionIV);
                        keyGen.init(128, secureRandom);
                        SecretKey sercetKey = keyGen.generateKey();

                        // byte[] encode_secretKey = parseHexStr2Byte(pwd);
                        // SecretKeySpec secretKey = new SecretKeySpec(encode_secretKey, "AES");

                        IvParameterSpec iv = new IvParameterSpec(EncryptionIV);
                        cipher.init(Cipher.DECRYPT_MODE, secretKey ,iv);

                        retByte = cipher.doFinal(SourceData);  

                } catch (NoSuchAlgorithmException e) {  

                        e.printStackTrace();  

                } catch (NoSuchPaddingException e) {  

                        e.printStackTrace();  

                } catch (InvalidKeyException e) {  
                        e.printStackTrace();  
                } catch (IllegalBlockSizeException e) {  
                        e.printStackTrace();  
                } catch (BadPaddingException e) {  
                        e.printStackTrace();  
                }  
                return retByte;
        }


        /**
        * 加密一个字符串
        * 
        * @param 原数据
        *            String
        * @throws Exception
        * @return 加密后的字符串 String
        */
        public String encrypAES(String SourceData){
                try{
                        String retStr = null;
                        byte[] retByte = null;
                        byte[] sorData = SourceData.getBytes(ENCODE);
                        retByte = EncryptionByteData(sorData);
                        BASE64Encoder be = new BASE64Encoder();
                        retStr = be.encode(retByte);
                        return retStr;
                }catch (Exception e) {
                        e.printStackTrace();
                        return "ERROR";
                }
        }

        /**
        * 加密一个字符串
        * @param SourceData 原数据
        * @param charsetName 字符集
        * @throws Exception
        * @return 加密后的字符串 String
        */
        public String encrypAES(String SourceData,String charsetName){
                try{
                        String retStr = null;
                        byte[] retByte = null;


                        byte[] sorData = SourceData.getBytes(charsetName);
                        retByte = EncryptionByteData(sorData);
                        BASE64Encoder be = new BASE64Encoder();
                        retStr = be.encode(retByte);
                        return retStr;
                }catch (Exception e) {
                        e.printStackTrace();
                        return "ERROR";
                }
        }


        /**
        * 字符串解密的方法
        * 
        * @param 原机密后的字符串
        *            String
        * @throws Exception
        * @return 解密后的字符串 String
        */
        public String decrypAES(String SourceData){
                try{
                        String retStr = null;
                        byte[] retByte = null;


                        BASE64Decoder bd = new BASE64Decoder();
                        byte[] sorData = bd.decodeBuffer(SourceData);
                        retByte = DecryptionByteData(sorData);
                        retStr = new String(retByte,ENCODE);
                        return retStr;
                }catch (Exception e) {
                        e.printStackTrace();
                        return "ERROR";
                }
        }

        /**
        * 字符串解密的方法
        * 
        * @param SourceData 原数据
        * @param charsetName 字符集
        * @throws Exception
        * @return 解密后的字符串 String
        */
        public String decrypAES(String SourceData, String charsetName){
                try{
                        String retStr = null;
                        byte[] retByte = null;


                                BASE64Decoder bd = new BASE64Decoder();
                                 byte[] sorData = bd.decodeBuffer(SourceData);
                                retByte = DecryptionByteData(sorData);
                                retStr = new String(retByte,charsetName);
                                return retStr;
                }catch (Exception e) {
                        e.printStackTrace();
                         return "ERROR";
                }
        }

//        private static byte[] parseHexStr2Byte(String hexStr) {
//                 if (hexStr.length() < 1)
//                          return null;
//        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;
//    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值