content: package com.jingewenku.abrahamcaijin.commonutil.encryption;
  
  import android.util.Base64;
  
  import javax.crypto.Cipher;
  import javax.crypto.KeyGenerator;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.SecretKeySpec;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  
  /**
   * @Description:主要功能:AES对称加密（Advanced Encryption Standard，高级数据加密标准，AES算法可以有效抵制针对DES的攻击算法，对称加密算法）
   * @Prject: CommonUtilLibrary
   * @Package: com.jingewenku.abrahamcaijin.commonutil.encryption
   * @author: AbrahamCaiJin
   * @date: 2017年05月16日 15:54
   * @Copyright: 个人版权所有
   * @Company:
   * @version: 1.0.0
   */
  
  public class AESUtils {
  
  
      /*
       * 生成密钥
       */
      public static byte[] initKey() throws NoSuchAlgorithmException {
          KeyGenerator keyGen = KeyGenerator.getInstance("AES");
          keyGen.init(256);  //192 256
          SecretKey secretKey = keyGen.generateKey();
          return secretKey.getEncoded();
      }
  
      /*
       * AES 加密
       */
      public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
          SecretKey secretKey = new SecretKeySpec(key, "AES");
  
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, secretKey);
          byte[] cipherBytes = cipher.doFinal(data);
          return cipherBytes;
      }
  
      /*
       * AES 解密
       */
      public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
          SecretKey secretKey = new SecretKeySpec(key, "AES");
  
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, secretKey);
          byte[] plainBytes = cipher.doFinal(data);
          return plainBytes;
      }
  
  
      private SecretKeySpec skeySpec;
      private Cipher cipher;
  
      public AESUtils(byte[] keyraw) throws Exception {
          if (keyraw == null) {
              byte[] bytesOfMessage = "".getBytes("UTF-8");
              MessageDigest md = MessageDigest.getInstance("MD5");
              byte[] bytes = md.digest(bytesOfMessage);
  
              skeySpec = new SecretKeySpec(bytes, "AES");
              cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
          } else {
  
              skeySpec = new SecretKeySpec(keyraw, "AES");
              cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  
          }
      }
  
      public AESUtils(String passphrase) throws Exception {
          byte[] bytesOfMessage = passphrase.getBytes("UTF-8");
          MessageDigest md = MessageDigest.getInstance("MD5");
          byte[] thedigest = md.digest(bytesOfMessage);
          skeySpec = new SecretKeySpec(thedigest, "AES");
  
          cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      }
  
      public AESUtils() throws Exception {
          byte[] bytesOfMessage = "".getBytes("UTF-8");
          MessageDigest md = MessageDigest.getInstance("MD5");
          byte[] thedigest = md.digest(bytesOfMessage);
          skeySpec = new SecretKeySpec(thedigest, "AES");
  
          skeySpec = new SecretKeySpec(new byte[16], "AES");
          cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      }
  
      /**
       * 加密:先AES再base64
       *
       * @param plaintext
       * @return
       * @throws Exception
       */
      public String encrypt(String plaintext) throws Exception {
  
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  
          byte[] ciphertext = cipher.doFinal(plaintext.getBytes("UTF-8"));
  
          return Base64.encodeToString(ciphertext, Base64.DEFAULT);
      }
  
      /**
       * 解密:先base64再AES
       *
       * @param ciphertext
       * @return
       * @throws Exception
       */
      public String decrypt(String ciphertext) throws Exception {
          cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  
          byte[] plain64text = Base64.decode(ciphertext, Base64.DEFAULT);
  
          byte[] plaintext = cipher.doFinal(plain64text);
  
          return new String(plaintext, "UTF-8");
      }
  
  }