content: /**
   * 对公众平台发送给公众账号的消息加解密示例代码.
   * 
   * @copyright Copyright (c) 1998-2014 Tencent Inc.
   */
  
  // ------------------------------------------------------------------------
  
  /**
   * 针对org.apache.commons.codec.binary.Base64，
   * 需要导入架包commons-codec-1.9（或commons-codec-1.8等其他版本）
   * 官方下载地址：http://commons.apache.org/proper/commons-codec/download_codec.cgi
   */
  package com.soecode.wxtools.util.crypto;
  
  import java.nio.charset.Charset;
  import java.util.Arrays;
  import java.util.Random;
  
  import javax.crypto.Cipher;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  
  import org.apache.commons.codec.binary.Base64;
  
  import com.soecode.wxtools.exception.AesException;
  
  
  public class WXBizMsgCrypt {
  	static Charset CHARSET = Charset.forName("utf-8");
  	Base64 base64 = new Base64();
  	byte[] aesKey;
  	String token;
  	String appId;
  
  	public WXBizMsgCrypt(String token, String encodingAesKey, String appId) throws AesException {
  		if (encodingAesKey.length() != 43) {
  			throw new AesException(AesException.IllegalAesKey);
  		}
  
  		this.token = token;
  		this.appId = appId;
  		aesKey = Base64.decodeBase64(encodingAesKey + "=");
  	}
  
  	byte[] getNetworkBytesOrder(int sourceNumber) {
  		byte[] orderBytes = new byte[4];
  		orderBytes[3] = (byte) (sourceNumber & 0xFF);
  		orderBytes[2] = (byte) (sourceNumber >> 8 & 0xFF);
  		orderBytes[1] = (byte) (sourceNumber >> 16 & 0xFF);
  		orderBytes[0] = (byte) (sourceNumber >> 24 & 0xFF);
  		return orderBytes;
  	}
  
  	int recoverNetworkBytesOrder(byte[] orderBytes) {
  		int sourceNumber = 0;
  		for (int i = 0; i < 4; i++) {
  			sourceNumber <<= 8;
  			sourceNumber |= orderBytes[i] & 0xff;
  		}
  		return sourceNumber;
  	}
  
  	String getRandomStr() {
  		String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  		Random random = new Random();
  		StringBuffer sb = new StringBuffer();
  		for (int i = 0; i < 16; i++) {
  			int number = random.nextInt(base.length());
  			sb.append(base.charAt(number));
  		}
  		return sb.toString();
  	}
  
  	String encrypt(String randomStr, String text) throws AesException {
  		ByteGroup byteCollector = new ByteGroup();
  		byte[] randomStrBytes = randomStr.getBytes(CHARSET);
  		byte[] textBytes = text.getBytes(CHARSET);
  		byte[] networkBytesOrder = getNetworkBytesOrder(textBytes.length);
  		byte[] appidBytes = appId.getBytes(CHARSET);
  
  		// randomStr + networkBytesOrder + text + appid
  		byteCollector.addBytes(randomStrBytes);
  		byteCollector.addBytes(networkBytesOrder);
  		byteCollector.addBytes(textBytes);
  		byteCollector.addBytes(appidBytes);
  
  		byte[] padBytes = PKCS7Encoder.encode(byteCollector.size());
  		byteCollector.addBytes(padBytes);
  
  		byte[] unencrypted = byteCollector.toBytes();
  
  		try {
  			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  			SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
  			IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16);
  			cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
  
  			byte[] encrypted = cipher.doFinal(unencrypted);
  
  			String base64Encrypted = base64.encodeToString(encrypted);
  
  			return base64Encrypted;
  		} catch (Exception e) {
  			e.printStackTrace();
  			throw new AesException(AesException.EncryptAESError);
  		}
  	}
  
  	String decrypt(String text) throws AesException {
  		byte[] original;
  		try {
  			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  			SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES");
  			IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
  			cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
  
  			byte[] encrypted = Base64.decodeBase64(text);
  
  			original = cipher.doFinal(encrypted);
  		} catch (Exception e) {
  			e.printStackTrace();
  			throw new AesException(AesException.DecryptAESError);
  		}
  
  		String xmlContent, from_appid;
  		try {
  			byte[] bytes = PKCS7Encoder.decode(original);
  
  			byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
  			int xmlLength = recoverNetworkBytesOrder(networkOrder);
  
  			xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET);
  			from_appid = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length),
  					CHARSET);
  		} catch (Exception e) {
  			e.printStackTrace();
  			throw new AesException(AesException.IllegalBuffer);
  		}
  
  		if (!from_appid.equals(appId)) {
  			throw new AesException(AesException.ValidateAppidError);
  		}
  		return xmlContent;
  
  	}
  
  	public String encryptMsg(String replyMsg, String timeStamp, String nonce) throws AesException {
  		String encrypt = encrypt(getRandomStr(), replyMsg);
  		if (timeStamp == "") {
  			timeStamp = Long.toString(System.currentTimeMillis());
  		}
  		String signature = SHA1.getSHA1(token, timeStamp, nonce, encrypt);
  		String result = XMLParse.generate(encrypt, signature, timeStamp, nonce);
  		return result;
  	}
  
  	public String decryptMsg(String msgSignature, String timeStamp, String nonce, String postData)
  			throws AesException {
  		Object[] encrypt = XMLParse.extract(postData);
  		String signature = SHA1.getSHA1(token, timeStamp, nonce, encrypt[1].toString());
  		if (!signature.equals(msgSignature)) {
  			throw new AesException(AesException.ValidateSignatureError);
  		}
  		String result = decrypt(encrypt[1].toString());
  		return result;
  	}
  
  	public String verifyUrl(String msgSignature, String timeStamp, String nonce, String echoStr)
  			throws AesException {
  		String signature = SHA1.getSHA1(token, timeStamp, nonce, echoStr);
  
  		if (!signature.equals(msgSignature)) {
  			throw new AesException(AesException.ValidateSignatureError);
  		}
  
  		String result = decrypt(echoStr);
  		return result;
  	}
  
  }