AES加密

/**
 * AES加密解密使用说明,因多处数据采用此种加密方式,请勿对该类中的方法及变量进行修改。
 * 使用该类只需使用这两个方法就可以,两方法的返回值均为字符串。
 * aesEncrypt(String str);向此方法中传入需要加密的数据,便会进行加密操作。
 * aesDecrypt(String str);向此方法中传入需要解密的数据,便会进行解密操作。
 */
import java.math.BigInteger;  
import java.security.MessageDigest;  
import java.security.SecureRandom;  
 
import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;  
import sun.misc.BASE64Encoder;  
public class AES {

    
      private static final String  key = "XAUM";//密钥******
        /**
         * base 64 encode
         * @param bytes 待编码的byte[]
         * @return 编码后的base 64 code
         */  
        public static String base64Encode(byte[] bytes){  
            return new BASE64Encoder().encode(bytes);  
        }  
          
        /**
         * base 64 decode
         * @param base64Code 待解码的base 64 code
         * @return 解码后的byte[]
         * @throws Exception
         */  
        public static byte[] base64Decode(String base64Code) throws Exception{
            if(base64Code != null && base64Code !="") {
                return new BASE64Decoder().decodeBuffer(base64Code);
           } else {
               return null;
           }
        }  
          
        /**
         * 获取byte[]的md5值
         * @param bytes byte[]
         * @return md5
         * @throws Exception
         */  
        public static byte[] md5(byte[] bytes) throws Exception {  
            MessageDigest md = MessageDigest.getInstance("MD5");  
            md.update(bytes);  
              
            return md.digest();  
        }  
          
        /**
         * 获取字符串md5值
         * @param msg  
         * @return md5
         * @throws Exception
         */  
        public static byte[] md5(String msg) throws Exception {  
            if(msg != null && msg !="") {
                   return  md5(msg.getBytes());
              } else {
                  return null;
              }
        }  
          
        /**
         * 结合base64实现md5加密
         * @param msg 待加密字符串
         * @return 获取md5后转为base64
         * @throws Exception
         */  
        public static String md5Encrypt(String msg) throws Exception{  
            if(msg != null && msg !="") {
                  return  base64Encode(md5(msg));
             } else {
                 return null;
             }
        }  
          
        /**
         * AES加密
         * @param content 待加密的内容
         * @param encryptKey 加密密钥
         * @return 加密后的byte[]
         * @throws Exception
         */  
        public static byte[] aesEncryptToBytes(String content) throws Exception {  
            String encryptKey =  AES.key;
            KeyGenerator kgen = KeyGenerator.getInstance("AES");  
            kgen.init(128, new SecureRandom(encryptKey.getBytes()));  
      
            Cipher cipher = Cipher.getInstance("AES");  
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
              
            return cipher.doFinal(content.getBytes("utf-8"));  
        }  
          
        /**
         * AES加密为base 64 code
         * @param content 待加密的内容
         * @param encryptKey 加密密钥
         * @return 加密后的base 64 code
         * @throws Exception
         */  
        public static String aesEncrypt(String content) throws Exception {  
            return base64Encode(aesEncryptToBytes(content));  
        }  
          
        /**
         * AES解密
         * @param encryptBytes 待解密的byte[]
         * @param decryptKey 解密密钥
         * @return 解密后的String
         * @throws Exception
         */  
        public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {  
            String decryptKey =  AES.key;
            KeyGenerator kgen = KeyGenerator.getInstance("AES");  
            kgen.init(128, new SecureRandom(decryptKey.getBytes()));  
              
            Cipher cipher = Cipher.getInstance("AES");  
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
            byte[] decryptBytes = cipher.doFinal(encryptBytes);  
              
            return new String(decryptBytes);  
        }  
          
        /**
         * 将base 64 code AES解密
         * @param encryptStr 待解密的base 64 code
         * @param decryptKey 解密密钥
         * @return 解密后的string
         * @throws Exception
         */  
        public static String aesDecrypt(String encryptStr) throws Exception {  
            if(encryptStr != null && encryptStr !="") {
                 return aesDecryptByBytes(base64Decode(encryptStr));
            } else {
                return null;
            }
        }  
}

原文地址:https://www.cnblogs.com/guokai870510826/p/5674545.html