AES加解密

package com.ai.runner.center.bmc.util;

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

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

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


/**
 * 加解密工具类 Date: 2017年02月12日 <br>
 * 
 * @author zhoushanbin
 * 
 */
public final class CryptoUtils {
	
	private static Logger LOG = LoggerFactory.getLogger(CryptoUtils.class);
	
	/**
	 * aes 加密模式为AES/CBC/PKCS5Padding
	 */
	public static final String AES_CBC_PAD = "AES/CBC/PKCS5Padding";
	
	/**
	 * AES
	 */
	public static final String AES = "AES";
	/**
	 * 初始化向量长度
	 */
	public static final int IV_LENGTH = 16;
	
	/**
	 * 编码方式
	 */
	public static final String UTF_8 = "utf-8";
	
	/**
	 * 工作密钥 16字节数组
	 */
	public static final byte[] WORK_KEY = { (byte) 0x11, (byte) 0x22,
			(byte) 0x33, (byte) 0x22, (byte) 0x13, (byte) 0xff, (byte) 0xee,
			(byte) 0x44, (byte) 0xaa, (byte) 0xcc, (byte) 0x16, (byte) 0x57,
			(byte) 0x00, (byte) 0x40, (byte) 0xba, (byte) 0xbe, };

	
	/**
	 * 加密
	 * @param content
	 * @return
	 */
	public static String encrypt(String content) {
		
		if(StringUtils.isEmpty(content)){
			return null;
		}
		
		SecretKeySpec skeySpec = new SecretKeySpec(WORK_KEY, AES);
		Cipher cipher = null;
		byte []result  = null;
		String afCnt = null;
		try {
			cipher = Cipher.getInstance(AES_CBC_PAD);
			byte []iv = getIv();
			
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
			byte[] cnt = cipher.doFinal(content.getBytes());
			
			result = new byte[cnt.length+IV_LENGTH];
			System.arraycopy(iv, 0, result, 0, iv.length);
			System.arraycopy(cnt, 0, result, IV_LENGTH, cnt.length);
			afCnt = new String(Base64.encodeBase64(result),UTF_8);
			
		} catch (NoSuchAlgorithmException e) {
			LOG.error("加密失败",e);
		} catch (NoSuchPaddingException e) {
			LOG.error("加密失败",e);
		} catch (InvalidKeyException e) {
			LOG.error("加密失败",e);
		} catch (InvalidAlgorithmParameterException e) {
			LOG.error("加密失败",e);
		} catch (IllegalBlockSizeException e) {
			LOG.error("加密失败",e);
		} catch (BadPaddingException e) {
			LOG.error("加密失败",e);
		} catch (UnsupportedEncodingException e) {
			LOG.error("加密失败",e);
		}
		
		return afCnt;
	}
	
	/**
	 * 解密
	 * @param content
	 * @return
	 */
	public static String decrypt(String content) {
		
		if(StringUtils.isEmpty(content)){
			return null;
		}
		try {
			byte[] acnt = content.getBytes(UTF_8);
			
			byte[] bcnt = Base64.decodeBase64(acnt);
			
			byte []iv = new byte[IV_LENGTH];
			byte []cnt = new byte[bcnt.length-IV_LENGTH];
			System.arraycopy(bcnt, 0, iv, 0, IV_LENGTH); 
			System.arraycopy(bcnt, IV_LENGTH, cnt, 0, bcnt.length-IV_LENGTH);
			
			//进行解密操作
			SecretKeySpec skeySpec = new SecretKeySpec(WORK_KEY, AES);
			Cipher cipher = Cipher.getInstance(AES_CBC_PAD);
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
			byte[] original = cipher.doFinal(cnt);
			return new String(original,UTF_8);
			
		} catch (UnsupportedEncodingException e) {
			LOG.error("加密失败",e);
		} catch (NoSuchAlgorithmException e) {
			LOG.error("加密失败",e);
		} catch (NoSuchPaddingException e) {
			LOG.error("加密失败",e);
		} catch (InvalidKeyException e) {
			LOG.error("加密失败",e);
		} catch (InvalidAlgorithmParameterException e) {
			LOG.error("加密失败",e);
		} catch (IllegalBlockSizeException e) {
			LOG.error("加密失败",e);
		} catch (BadPaddingException e) {
			LOG.error("加密失败",e);
		}
		
		return null;
	}
	
	/**
	 * 获取初始化向量 16个字节
	 * @return
	 */
	private static byte[] getIv(){
		SecureRandom random = new SecureRandom();
		byte []iv = new byte[IV_LENGTH];
		random.nextBytes(iv);
		return iv;
	}
		
    public static String getMd5(String str) {
    	MessageDigest md5;
    	StringBuilder sb = new StringBuilder(40);
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] bs = md5.digest(str.getBytes());
        
			for(byte x:bs) {
				if((x & 0xff)>>4 == 0) {
					sb.append("0").append(Integer.toHexString(x & 0xff));
				} else {
					sb.append(Integer.toHexString(x & 0xff));
				}
			}
		} catch (NoSuchAlgorithmException e) {
			LOG.error("获取MD5失败",e);
		}
        return sb.toString().toUpperCase();
    }
	
    
    public static String getMd5(String str,int iterations){
    	if(iterations <= 1){
    		return getMd5(str);
    	}
    	for(int i=1;i<=iterations;i++){
    		str = getMd5(str);
    	}
    	return str;
    }
    
	public static void main(String args[]){

	
		System.out.println(getMd5("0",10000));
		System.out.println(getMd5("qws3@!@#",1));
		System.out.println(getMd5("qws3@!@#",2));
		System.out.println(getMd5("qws3@!@#",3));
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值