package com.usoftchina.bi.core.utils;

import org.springframework.stereotype.Component;

@Component
public class EncryUtil {
	//人员资料 密码  des加密默认key
	private final static String key = "96878265";
	//BI  报表编号  des加密默认key
    private final static String BICODEKEY = "usoftChina-BI";
	
	/**
	 * 获取加密工具类DesUtil
	 * @return
	 * @throws Exception
	 */
	private static DesUtil getDesUtil() throws Exception{
		return new DesUtil(key);
	}

    /**
     * 获取加密工具类DesUtil
     * @param encryKey
     * @return
     * @throws Exception
     */
    private static DesUtil getDesUtil(String encryKey) throws Exception{
        return new DesUtil(encryKey);
    }
	
	/**
	 * 给密码加盐
	 * @param password 密码
	 * @param mobile 手机号
	 * @return
	 */
	private static String setPasswordSalt(String password,String mobile){
		return mobile+"{"+password+"}";
	}
	
	/**
	 * 获取密码
	 * @param password
	 * @return
	 */
	private static String removePasswordSalt(String password){
		return password.substring(password.indexOf("{")+1, password.length()-1);
	}
	/**
	 * 对密码进行加密
	 * @param password 密码
	 * @param mobile   手机号
	 * @return 加密后的密码
	 * @throws Exception
	 */
	public static String encryptPassword(String password,String mobile){
		try {
			return getDesUtil().encrypt(setPasswordSalt(password,mobile));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return password;
	}
	
	/**
	 * 对密码进行解密
	 * @param password 加密的密码
	 * @return 明文密码
	 * @throws Exception
	 */
	public  static String decryptPassword(String password){
		try {
			String pass = getDesUtil().decrypt(password);
			return removePasswordSalt(pass);
		} catch (Exception e) {

		}
		return password;
	}

    /**
     * 对message进行加密
     * @return 加密后的密文
     * @throws Exception
     */
    public static String encryptBiCode(String message){
        try {
            return getDesUtil(BICODEKEY).encrypt(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

    /**
     * 对密码进行解密
     * @param message 加密的message
     * @return 解密后的message
     * @throws Exception
     */
    public  static String decryptBiCode(String message){
        try {
            String desMessage = getDesUtil(BICODEKEY).decrypt(message);
            return desMessage;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

}
