AES对数据加密 笔记



package com.yy.gpo.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;


/**
 * 共同加解密方法
 *
 * @author Administrator
 */
public class EncryptUtil {
 private static final String UTF_8 = "utf-8";
 private static final String AES = "AES";
 
 public static final String SIGN_ALGORITHMS = "SHA1PRNG";

 
 static String SECRET_PWD = "";

 static {
  // 获取密钥
  try {
   String str = "1234567890abcdef";
   byte[] b = str.getBytes(UTF_8);
   SECRET_PWD = new String(new Base64().encode(b));
   //System.out.println("Base64加密后的密钥密码:" + SECRET_PWD);
  } catch (UnsupportedEncodingException e) {
   
  }
 }

 /**
  * AES加密
  * <p>返回 Base64 加密结果 code</p>
  * @param content 待加密的内容
  * @return 加密后的base 64 code
  * @throws Exception
  */
 public static String encrypt(String content) {
  if(StringUtils.isBlank(content)){
    return null;
   }
  try {
   // 用AES算法加密的密钥
   SecretKeySpec key = getKey();
   // 创建密码器
   Cipher cipher = Cipher.getInstance(AES);
   cipher.init(Cipher.ENCRYPT_MODE, key);
   byte[] byteEncrypt = cipher.doFinal(content.getBytes(UTF_8));
   return base64Encode(byteEncrypt);
  } catch (NoSuchAlgorithmException e) {
   
   return null;
  } catch (NoSuchPaddingException e) {
   
   return null;
  } catch (InvalidKeyException e) {
   
   return null;
  } catch (IllegalBlockSizeException e) {
   
   return null;
  } catch (BadPaddingException e) {
   
   return null;
  } catch (UnsupportedEncodingException e) {
   
   return null;
  }
 }

 /**
  * AES解密
  * <p>Base64结果解密 </p>
  * @param content 待解密的base 64 code
  * @return 解密后的string
  * @throws Exception
  */
 public static String decrypt(String content) {
   if(StringUtils.isBlank(content)){
    return null;
   }
  try {
   // 用AES算法加密的密钥
   SecretKeySpec key = getKey();
   // 创建密码器
   Cipher cipher = Cipher.getInstance(AES);
//   Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
   cipher.init(Cipher.DECRYPT_MODE, key);
   byte[] byteDecrypt = cipher.doFinal(base64Decode(content));
   return new String(byteDecrypt);
  } catch (NoSuchAlgorithmException e) {
   
   return null;
  } catch (NoSuchPaddingException e) {
   
   return null;
  } catch (IllegalBlockSizeException e) {
   
   return null;
  } catch (BadPaddingException e) {
   
   return null;
  } catch (UnsupportedEncodingException e) {
   
   return null;
  } catch (InvalidKeyException e) {
   
   return null;
  }
 }
 
 /**
  * 生成密钥
  *
  * @return SecretKeySpec 用AES算法加密的密钥
  * @throws NoSuchAlgorithmException
  * @throws UnsupportedEncodingException
  */
 private static SecretKeySpec getKey() throws NoSuchAlgorithmException, UnsupportedEncodingException {
  //实例化一个用AES加密算法的密钥生成器
  KeyGenerator kgen = KeyGenerator.getInstance(AES);
//  kgen.init(128, new SecureRandom(SECRET_PWD.getBytes()));
  
  SecureRandom random = SecureRandom.getInstance(SIGN_ALGORITHMS);
        random.setSeed(SECRET_PWD.getBytes(UTF_8));
        kgen.init(128, random);

  //生成一个密钥
  SecretKey secretKey = kgen.generateKey();
  SecretKeySpec key = new SecretKeySpec(secretKey.getEncoded(), AES);
  return key;
 }

 /**
  * base 64 encode
  *
  * @param bytes
  *            待编码的byte[]
  * @return 编码后的base 64 code
  */
 private static String base64Encode(byte[] bytes) {
  return new String(new Base64().encode(bytes));
 }

 /**
  * base 64 decode
  *
  * @param base64Code
  *            待解码的base 64 code
  * @return 解码后的byte[]
  * @throws UnsupportedEncodingException
  * @throws Exception
  */
 private static byte[] base64Decode(String base64Code) throws UnsupportedEncodingException {
  return StringUtil.isEmpty(base64Code) ? null : new Base64().decode(base64Code.getBytes(UTF_8));
 }

 public static void main(String[] args) {
//  String test = "123456.23";
//  System.out.println(EncryptUtil.encrypt(test));
  System.out.println(EncryptUtil.decrypt("x3+l/ydZYo+JKS5Mji5ohA=="));
//  String test_jiami = EncryptUtil.encrypt(test);03xzaerJs88o86cuFKtAMQ==  5JpfBOgawSonvKjNR0i5mw==  ks79RzARRV3jNsUPZ1NXwQ==

//  System.out.println(EncryptUtil.decrypt("YZUMpSCefN7FAr//il66Ww=="));
 }
}

猜你喜欢

转载自blog.csdn.net/qw222pzx/article/details/80984480
今日推荐