Java-AES加密解密

package com.jy.demo.web;


import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AESUtils{
  public static final String IV = "2015030120123456";
  private static final Logger logger = LoggerFactory.getLogger(AESUtils.class);

  public static byte[] aesEncrypt(String content, String pkey)
  {
    try
    {
      SecretKeySpec key = new SecretKeySpec(pkey.getBytes(), "AES");
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      IvParameterSpec iv = new IvParameterSpec("2015030120123456".getBytes());
      cipher.init(1, key, iv);
      return cipher.doFinal(content.getBytes("UTF-8"));
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  private static SecretKey generateKey(String secretKey)
    throws Exception
  {
    Provider p = Security.getProvider("SUN");
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", p);
    secureRandom.setSeed(secretKey.getBytes());
    KeyGenerator kg = KeyGenerator.getInstance("AES");
    kg.init(secureRandom);

    return kg.generateKey();
  }

  public static String aesEncryptStr(String content, String pkey)
  {
    byte[] aesEncrypt = aesEncrypt(content, pkey);
    logger.debug("aesEncrypt String:" + Arrays.toString(aesEncrypt));
    String base64EncodeStr = Base64.encodeBase64String(aesEncrypt);
    logger.debug("aesEncrypt base64EncodeStr:" + base64EncodeStr);
    return base64EncodeStr;
  }

  public static String aesDecodeStr(String content, String pkey)
    throws Exception
  {
    byte[] base64DecodeStr = Base64.decodeBase64(content);
    logger.debug("base64DecodeStr String:" + Arrays.toString(base64DecodeStr));
    byte[] aesDecode = aesDecode(base64DecodeStr, pkey);
    if (aesDecode == null) {
      return null;
    }
    String result = new String(aesDecode, "UTF-8");
    logger.debug("aesDecode result:" + result);
    return result;
  }

  public static byte[] aesDecode(byte[] content, String pkey)
  {
    try
    {
      SecretKeySpec key = new SecretKeySpec(pkey.getBytes(), "AES");
      IvParameterSpec iv = new IvParameterSpec("2015030120123456".getBytes("UTF-8"));
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      cipher.init(2, key, iv);
      return cipher.doFinal(content);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  
  public static void main(String[] args) throws Exception {
      System.out.println( AESUtils.aesEncryptStr("123456", "abcdnnnnnn123456"));//根据key加密
      System.out.println( AESUtils.aesDecodeStr("i/urUHvLlepO+NWthBU7AA==", "abcdnnnnnn123456"));//根据加密后密码还有加密key解密
}
  

}

 

posted @ 2016-04-25 11:14  也许还年轻  阅读(869)  评论(0编辑  收藏  举报