package com.fs.his.utils;

import com.fs.common.utils.ParseUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class PhoneUtil {

    public static String encryptPhone(String text) {
        String encryptedText=null;
        try {
            SecretKeySpec secretKey = new SecretKeySpec("AESAabCdeREssREA".getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            // Encryption
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedBytes = cipher.doFinal(text.getBytes());
            encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedText;
    }
    /**
    * 解密
    */
    public static String decryptPhone(String encryptedText) {
        String text=null;
        try {
            SecretKeySpec secretKey = new SecretKeySpec("AESAabCdeREssREA".getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
            text = new String(decryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return text;
    }
    /**
    * 解密加*
    */
    public static String decryptPhoneMk(String encryptedText) {
        String text=null;
        try {
            SecretKeySpec secretKey = new SecretKeySpec("AESAabCdeREssREA".getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
            text = new String(decryptedBytes);
            text =text.replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return text;
    }

    public static String decryptAutoPhoneMk(String encryptedText) {
        String text=null;
        if (encryptedText!=null&&encryptedText!="") {
            if (encryptedText.length()>11){
                try {
                    SecretKeySpec secretKey = new SecretKeySpec("AESAabCdeREssREA".getBytes(), "AES");
                    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                    cipher.init(Cipher.DECRYPT_MODE, secretKey);
                    byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
                    text = new String(decryptedBytes);
                    text =text.replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                text =  ParseUtils.parsePhone(encryptedText);
            }
        }

        return text;
    }
    /**
     * 用于查询 使用老的数据加密
     * @param text
     * @return
     */
    private static String OLD_KEY = "2c8d1a7f4e9b3c6ae6d5c4b3a291f8c9";
    public static String encryptPhoneOldKey(String text) {
        String encryptedText=null;
        try {
            SecretKeySpec secretKey = new SecretKeySpec(OLD_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            // Encryption
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedBytes = cipher.doFinal(text.getBytes());
            encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedText;
    }

    /**
     * 与 {@link #encryptPhoneOldKey(String)} 对应的解密
     */
    public static String decryptPhoneOldKey(String encryptedText) {
        String text = null;
        if (encryptedText == null || encryptedText.isEmpty()) {
            return null;
        }
        try {
            SecretKeySpec secretKey = new SecretKeySpec(OLD_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
            text = new String(decryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return text;
    }

    public static void main(String[] args) {
        System.out.println(encryptPhone("19111374008"));
    }
}
