aes加解密数据项

使用aes加解密需要把jdk和jre中的lib/security下的local_policy.jar 和 US_export_policy.jar替换掉

JDK6的下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html

JDK7的下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

JDK8的下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

package com.fh.util;

import java.io.*;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;


public class AesUtils {
	private static final String CipherMode = "AES/CBC/PKCS5Padding";

	// /** 创建密钥 **/
	private static SecretKeySpec createKey(String key) {
		byte[] data = null;
		if (key == null) {
			key = "";
		}
		StringBuffer sb = new StringBuffer(16);
		sb.append(key);
		while (sb.length() < 16) {
			sb.append("0");
		}
		if (sb.length() > 16) {
			sb.setLength(16);
		}

		try {
			data = sb.toString().getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return new SecretKeySpec(data, "AES");
	}

	private static IvParameterSpec createIV(String password) {
		byte[] data = null;
		if (password == null) {
			password = "";
		}
		StringBuffer sb = new StringBuffer(16);
		sb.append(password);
		while (sb.length() < 16) {
			sb.append("0");
		}
		if (sb.length() > 16) {
			sb.setLength(16);
		}

		try {
			data = sb.toString().getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return new IvParameterSpec(data);
	}

	// /** 加密字节数据 **/
	public static byte[] encrypt(byte[] content, String password, String iv) {
		try {
			SecretKeySpec key = createKey(password);
			Cipher cipher = Cipher.getInstance(CipherMode);
			cipher.init(Cipher.ENCRYPT_MODE, key, createIV(iv));
			byte[] result = cipher.doFinal(content);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// /** 加密(结果为16进制字符串) **/
	public static String encrypt(String content, String password, String iv) {
		byte[] data = null;
		try {
			data = content.getBytes("UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data = encrypt(data, password, iv);
		String result = new Base64().encodeToString(data);
		return result;
	}

	// /** 解密字节数组 **/
	public static byte[] decrypt(byte[] content, String password, String iv) {
		try {
			SecretKeySpec key = createKey(password);
			Cipher cipher = Cipher.getInstance(CipherMode);
			cipher.init(Cipher.DECRYPT_MODE, key, createIV(iv));
			byte[] result = cipher.doFinal(content);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// /** 解密 **/
	public static String decrypt(String content, String password, String iv) {
		byte[] data = null;
		try {
			data = new Base64().decode(content);// 先用base64解密
		} catch (Exception e) {
			e.printStackTrace();
		}
		data = decrypt(data, password, iv);
		if (data == null)
			return null;
		String result = null;
		try {
			result = new String(data, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
        String content = "测试中文转码,如果没有转成功,那就再试试!";
        String password = "12345678";
        // 加密
        //System.out.println("加密前:" + content);
        String encode = encrypt(content, password,null);
        System.out.println(encode);
        // 解密
        String decryptResult = decrypt(encode, password,null);
        System.out.println(decryptResult);
        //System.out.println("解密后:" + new String(decryptResult.getBytes("ISO-8859-1"), "UTF-8")); //不转码会乱码
         
    }

}

新增,修改,导入一切更新数据的时候,进行加密处理;

if (null != pd.getString("JIATINGZHUZHI") && !"".equals(pd.getString("JIATINGZHUZHI"))) {
	pd.put("JIATINGZHUZHI", AesUtils.encrypt(pd.getString("JIATINGZHUZHI"), null,null));
		}

一切需要显示数据的时候,进行解密处理;

if (null != pd.getString("JIATINGZHUZHI") && !"".equals(pd.getString("JIATINGZHUZHI"))) {
	pd.put("JIATINGZHUZHI", AesUtils.decrypt(pd.getString("JIATINGZHUZHI"), null,null));
		}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值