package weaver.fna.invoice.utils;

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

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * @Description:·¢Æ±ÔÆ¹¤¾ßÀà
 */
public class InvoiceCloudAESUtil{
	
	
	
	
	public static String startAssemble(String aesKey,String password,String bodyJson) throws Exception {
		String bodyStr = encrypt(aesKey,bodyJson.toString());
		String content = invoiceCloudEncode(bodyStr.toString(), password);
		return content;
		
	}
//	public static void main(String[] args) {
//		try {
//			
//			String bodyJson = "{\"cid\":\"766941083040768000\",\"userId\":1,\"flag\":\"3\",\"fids\":[\"868117722088693763\"]}";
//			String aesKey = "w34xx413y2x23w4y";
//			String userName = "1438490100786012161";
//			String password = "80c8664aa7a444049b8ed49ed5095561";
//			String bodyStr = encrypt(aesKey,bodyJson.toString());
//			System.out.println(bodyStr);
//			String content = invoiceCloudEncode(bodyStr.toString(), password);
//			System.out.println(content);
//			Map<String, String> map = new HashMap<String, String>();
//
//			map.put("appkey", userName);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

    private static byte[] encrypt(String key, byte[] origData) throws Exception {
        byte[] keyBytes = getKeyBytes(key);
        byte[] buf = new byte[16];
        System.arraycopy(keyBytes, 0, buf, 0, keyBytes.length > buf.length ? keyBytes.length : buf.length);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(buf, "AES"), new IvParameterSpec(keyBytes));
        return cipher.doFinal(origData);

    }

    private static byte[] decrypt(String key, byte[] crypted) throws Exception {
        byte[] keyBytes = getKeyBytes(key);
        byte[] buf = new byte[16];
        System.arraycopy(keyBytes, 0, buf, 0, keyBytes.length > buf.length ? keyBytes.length : buf.length);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(buf, "AES"), new IvParameterSpec(keyBytes));
        return cipher.doFinal(crypted);
    }

    private static byte[] getKeyBytes(String key) throws Exception {
        byte[] bytes = key.getBytes("utf8");
        return bytes;
    }

    public static String encrypt(String key, String val) throws Exception {
        byte[] origData = val.getBytes("utf8");
        byte[] crypted = encrypt(key, origData);
        return new String(Base64.encodeBase64(crypted), "UTF-8");
    }

    public static String decrypt(String key, String val) throws Exception {
        byte[] crypted = Base64.decodeBase64(val.getBytes("utf8"));
        byte[] origData = decrypt(key, crypted);
        return new String(origData, "utf8");
    }
    
    private static String invoiceCloudEncode(String content,String secret) throws Exception {
    	long time = System.currentTimeMillis();
    	content = content+"&time="+time;
    	String md5Content =  DigestUtils.md5Hex(content + "&secret=" + secret);
    	content = content+"&md5="+md5Content;
    	return content;
    }
}