AesCBC.java 3.5 KB
package com.framework.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.jfinal.kit.HttpKit;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AesCBC {
	/*
	 * 已确认 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
	 */
//	public final static String sKey = "aaaaaaaaaaaaaaaa";
	public final static String ivParameter = "0000000000000000";
	private static AesCBC instance = null;

	// private static
	private AesCBC() {

	}

	public static AesCBC getInstance() {
		if (instance == null)
			instance = new AesCBC();
		return instance;
	}

	public static String getKey() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date = new Date();
		String formatDate = sdf.format(date);
		Date d = new Date(System.currentTimeMillis());
		String key = formatDate + "owenkasduf" + (new SimpleDateFormat("MM")).format(d);
		return key;
	}

	// 加密
	public String encrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) {
		try {
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			byte[] raw = sKey.getBytes();
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
//	        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));
			return new BASE64Encoder().encode(encrypted);// 此处使用BASE64做转码。
		} catch (Exception e) {
		}

		return null;
	}

	// 解密
	public String decrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) {
		try {
			byte[] raw = sKey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
//            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original, encodingFormat);
			return originalString;
		} catch (Exception ex) {
			return null;
		}
	}

	public static void main(String[] args) throws Exception {
		// 需要加密的字串
//		String cSrc = "aqswed1234";
//		System.out.println("加密前的字串是:" + cSrc);
//		// 加密
//		String enString = AesCBC.getInstance().encrypt(cSrc, "utf-8", getKey(), ivParameter);
//		System.out.println("加密后的字串是:" + enString);
//        BASE64Encoder encoder = new BASE64Encoder();
//		List<NameValuePair> params = new ArrayList<NameValuePair>();
//		params.add(new BasicNameValuePair("data", enString));
		String params = "data=YCbPytqeduiEj0PGGIeF5Q4rgxJNd59JCtJ5duHrzN8bKvlc8wGhqVyCraamqwNCwb+eGhCBoZ3v\r\n" + 
				"6SD39qXHKZTDCh/69wufSze7D1b9KMQ=";
		String result = HttpKit.post("http://zzcargo.com:8080/index.php?r=api/index", params);
		System.out.println(result);
		// 解密
//        String DeString = AesCBC.getInstance().decrypt(result,"utf-8",sKey,ivParameter);
//        System.out.println("解密后的字串是:" + DeString);
	}
}