/**
 * http://surenpi.com
 */
package org.suren.autotest.web.framework.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

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

/**
 * 加解密工具类
 * @author suren
 * @date 2016年12月24日 下午3:45:52
 */
public class EncryptorUtil
{
	public static final String ENCRYPT_KEY = "encrypt.key";
	
	/**
	 * @return 从配置文件中获取私钥
	 */
	private static String getSecretKey()
	{
		try(InputStream input = EncryptorUtil.class.getClassLoader().getResourceAsStream("encrypt.properties"))
		{
			if(input == null)
			{
				throw new RuntimeException("Can not found encrypt.properties!");
			}
			
			Properties encryptPro = new Properties();
			encryptPro.load(input);
			
			if(!encryptPro.containsKey(ENCRYPT_KEY))
			{
				throw new RuntimeException("Can not found " + ENCRYPT_KEY + " from encrypt.properties!");
			}
			
			return encryptPro.getProperty(ENCRYPT_KEY);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return null;
	}

	/**
	 * 加密字符串
	 * @param plainText
	 * @return
	 */
	public static String encrypt(String plainText)
	{
		try
		{
			return Encryptor.getInstance(Encryptor.ALG_DES, getSecretKey()).encryptStr(plainText);
		}
		catch (InvalidKeyException e)
		{
			e.printStackTrace();
		}
		catch (IllegalBlockSizeException e)
		{
			e.printStackTrace();
		}
		catch (BadPaddingException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchPaddingException e)
		{
			e.printStackTrace();
		}
		
		return plainText;
	}
	
	/**
	 * 解密字符串
	 * @param encryptText
	 * @return
	 */
	public static String decrypt(String encryptText)
	{
		try
		{
			return Encryptor.getInstance(Encryptor.ALG_DES, getSecretKey()).decryptStr(encryptText);
		}
		catch (InvalidKeyException e)
		{
			e.printStackTrace();
		}
		catch (IllegalBlockSizeException e)
		{
			e.printStackTrace();
		}
		catch (BadPaddingException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchPaddingException e)
		{
			e.printStackTrace();
		}
		
		return encryptText;
	}
	
	/**
	 * 先加密后再进行Base64编码
	 * @param plainText
	 * @return
	 */
	public static String encryptWithBase64(String plainText)
	{
		try
		{
			return Base64.encodeBase64String(Encryptor.getInstance(Encryptor.ALG_DES, getSecretKey()).encrypt(plainText));
		}
		catch (InvalidKeyException e)
		{
			e.printStackTrace();
		}
		catch (IllegalBlockSizeException e)
		{
			e.printStackTrace();
		}
		catch (BadPaddingException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchPaddingException e)
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 先使用Base64解码然后解密
	 * @param base64Text
	 * @return
	 */
	public static String decryptWithBase64(String base64Text)
	{
		byte[] encryptData = Base64.decodeBase64(base64Text);
		try
		{
			return Encryptor.getInstance(Encryptor.ALG_DES, getSecretKey()).decryptStr(encryptData);
		}
		catch (InvalidKeyException e)
		{
			e.printStackTrace();
		}
		catch (IllegalBlockSizeException e)
		{
			e.printStackTrace();
		}
		catch (BadPaddingException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchPaddingException e)
		{
			e.printStackTrace();
		}
		
		return base64Text;
	}
}

@@ -134,16 +134,38 @@
 tream;%0D%0A
+import java.net.URL;%0D%0A
 import j
@@ -191,32 +191,32 @@
 dKeyException;%0D%0A
-
 import java.secu
@@ -239,32 +239,63 @@
 ithmException;%0D%0A
+import java.util.Enumeration;%0D%0A
 import java.util
@@ -745,29 +745,29 @@
 %0D%0A%09%09
-try(InputStream input
+ClassLoader clsLoader
  = E
@@ -805,49 +805,370 @@
 er()
-.getResourceAsStream(%22encrypt.properties%22
+;%0D%0A%09%09Enumeration%3CURL%3E urls = null;%0D%0A%09%09try%0D%0A%09%09%7B%0D%0A%09%09%09urls = clsLoader.getResources(%22encrypt.properties%22);%0D%0A%09%09%7D%0D%0A%09%09catch (IOException e1)%0D%0A%09%09%7B%0D%0A%09%09%09e1.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09if(urls == null %7C%7C !urls.hasMoreElements())%0D%0A%09%09%7B%0D%0A%09%09%09throw new RuntimeException(%22Can not found encrypt.properties!%22);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09try(InputStream input = urls.nextElement().openStream(
 ))%0D%0A

解决如果把脚本打包成jar后，框架无法从外部加载密钥配置文件（https://git.oschina.net/surenpi/autotest.web.framework/issues/36）