package com.x.app.test;

import java.net.URLDecoder;
import java.net.URLEncoder;

import com.x.app.util.MD5;

import android.util.Base64;

public class BDCryptoUtils {

	public static String encrypt(String input) {
		String output = "";

		try {
			String rawKey = makeKey("bdsystemmonitor_2013@mic");
			String newKey = rawKey.substring(rawKey.length() / 2);
			byte[] bytes = input.getBytes();
			byte[] encrypt = AES.encrypt("2013010111143000", newKey, bytes);
			String base64 = Base64.encode(encrypt, Base64.DEFAULT).toString();

			output = URLEncoder.encode(base64, "utf-8");
		} catch (Exception e) {
		}

		return output;
	}

	public static String decrypt(String input) {
		String output = "";

		try {
			String rawKey = makeKey("bdsystemmonitor_2013@mic");
			String newKey = rawKey.substring(rawKey.length() / 2);
			String base64 = URLDecoder.decode(input, "utf-8");
			byte[] encrypt = Base64.decode(base64.getBytes(), Base64.DEFAULT);

			output = AES.decrypt("2013010111143000", newKey, encrypt).toString();
		} catch (Exception e) {
		}

		return output;
	}

	private static String makeKey(String input) {
		char[] map = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };

		byte[] md5 = MD5.getMD5(input.getBytes());
		StringBuilder s = new StringBuilder(2 * md5.length);
		for (int i = 0; i < md5.length; i++) {
			s.append(map[(0xF0 & md5[i]) >>> 4]);
			s.append(map[0xF & md5[i]]);
		}

		return s.toString();
	}
}
