package org.fouram.core.util;

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

/**
 * API加密
 */
public class APIEncodeUtil {
	
	private static final String SPLIT = "!!!";
	private static final String APPKEY = "4d6f6155ed8e89f3fdd6df5bb432a1f4"; // 唯一值不可以修改

	/**
	 * 加密
	 * @param data
	 * @param skey
	 * @return
	 */
	public static String encode(String skey){
		try {
			// 使用RSA私钥加密APPKEY
			String apiRsaKey = Base64.encodeBase64String(RSAOperator.encryptPriKey(APPKEY.getBytes()));
			// 使用AES密钥加密
			String aesAndApiRsaKey = AESOperator.getInstance().encrypt(apiRsaKey, skey);
			// 使用RSA公钥加密AES密钥
			String rsaSKey = Base64.encodeBase64String(RSAOperator.encryptPubKey(skey.getBytes()));
			
			return aesAndApiRsaKey + SPLIT + rsaSKey;
		} catch (Exception e) {
			return "";
		}
	}
}