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));
		}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章