Java AES 对称加密

package com.inspur.uc.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.inspur.uc.util.mail.Base64;

/**
 * AES 对称加密算法
 */
public class AESUtil {

	private static Log log = LogFactory.getLog(AESUtil.class);
	private static String aes_key_path = "aes.key";
	
	//加载密钥文件
	private static SecretKey privateKey = null;
	static{
		ObjectInputStream ois = null;
		try {
			InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(aes_key_path);
			ois = new ObjectInputStream(inputStream);
			privateKey = (SecretKey) ois.readObject();
		} catch (FileNotFoundException e) {
			log.error("未找到密钥文件:"+aes_key_path, e);
		} catch (IOException e) {
			log.error("读取密钥文件出错:"+aes_key_path, e);
		} catch (ClassNotFoundException e) {
			log.error(null, e);
		}finally{
			if(ois != null){
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 生成密钥并保存至文件中
	 * @param key
	 */
	private static void createKey() {
		try {
			//生成密钥
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom());  
			SecretKey secretKey = kgen.generateKey();  
			
			//将密钥写入文件
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(aes_key_path));
			oos.writeObject(secretKey);
			System.out.println("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}  
	}

	/**
	 * AES加密
	 * @param content
	 * @param key
	 * @return String
	 */
	public static String encode(String content, SecretKey key){
		try {
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE,key );
			byte[] b = cipher.doFinal(content.getBytes());
			return Base64.encodeBytes(b);
		} catch (Exception e) {
			log.error("加密失败:content="+content+",key="+key);
		}
		return null;
	}
	
	//使用密钥文件中的密钥进行加密解密
	public static String encode(String content){
		return encode(content,privateKey);
	}
	public static String decode(String miwen){
		return decode(miwen,privateKey);
	}

	/**
	 * AES解密
	 * @param miwen
	 * @param key
	 * @return String
	 */
	public static String decode(String miwen, SecretKey key){
		String str = null;
		try {
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, key);
			str = new String(cipher.doFinal(Base64.decode(miwen)));
		} catch (Exception e) {
			log.error("解密失败:content="+miwen+",key="+key);
		}
		return str;
	}
	public static void main(String[] args) {
		String yuanwen = "中国,山东123abc!@#¥%……&*(*()》《。,。、?测试";
		System.out.println("原文:"+yuanwen);
		String miwen = encode(yuanwen);
		System.out.println("密文:"+miwen);
		System.out.println("解密:"+decode(miwen));
	}
}

 输出结果:

原文:中国,山东123abc!@#¥%……&*(*()》《。,。、?测试
密文:AQPJKD3ycFbQE1ZcXinf8PVM1EMc3jqvQC+QwFcQu7UFeQSO2Javt0FCTl0CdJfx95ppi1W6vE/w
kI5s7TdUY+Rqrl4MLjBj+wf49kqOgN8=
解密:中国,山东123abc!@#¥%……&*(*()》《。,。、?测试

 

你可能感兴趣的:(加密解密)