//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.bcxin.risk.base.domain.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;

public class CipherDESUtil {

    public CipherDESUtil() {
    }

    public static byte[] encrypt(byte[] datasource, String password) throws Exception {
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(password.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(1, securekey, random);
        return cipher.doFinal(datasource);
    }

    public static byte[] decrypt(byte[] src, String password) throws Exception {
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(password.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(2, securekey, random);
        return cipher.doFinal(src);
    }


    public static String parseByte2HexStr(byte[] buf) {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < buf.length; ++i) {
            String hex = Integer.toHexString(buf[i] & 255);
            if(hex.length() == 1) {
                hex = '0' + hex;
            }

            sb.append(hex.toUpperCase());
        }

        return sb.toString();
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if(hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }

            return result;
        }
    }

    /**
     * 以特定的方式加密字符串
     * @param str
     * @return
     */
    public static String encrypt(String str){
        byte[] buf = new byte[0];
        try {
            buf = CipherDESUtil.encrypt(str.getBytes(), "PSSPLATFORM");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CipherDESUtil.parseByte2HexStr(buf);
    }

    /**
     * 以特定的方式加密字符串
     * @param str
     * @return
     */
    public static String encryptLong(Long str){
        byte[] buf = new byte[0];
        try {
            buf = CipherDESUtil.encrypt(str.toString().getBytes(), "PSSPLATFORM");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CipherDESUtil.parseByte2HexStr(buf);
    }


    /**
     * 以特定的方式解密字符串
     * @param str
     * @return
     */
    public static String decrypt(String str){

        String newStr = null;
        try {
            newStr = new String(CipherDESUtil.decrypt(CipherDESUtil.parseHexStr2Byte(str), "PSSPLATFORM"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newStr;
    }
}
