package com.bonus.utils;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.PrimitiveArrayUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SM4EncryptUtils {
   private static final Logger log = LoggerFactory.getLogger(SM4EncryptUtils.class);
   private static final DataEncDecProperties properties = getProperties();
   public static final String SUFFIX = "##encrypted";

   public static DataEncDecProperties getProperties() {
      DataEncDecProperties dataEncDecProperties = new DataEncDecProperties();
      dataEncDecProperties.setSm4Key("jY7bZz6Pjml+H/WZYfNSNA==");
      dataEncDecProperties.setEncrypted(true);
      dataEncDecProperties.setDesensitization(true);
      return dataEncDecProperties; //(DataEncDecProperties)SpringContextHolder.getBean(DataEncDecProperties.class);
   }

   public static String sm4Encrypt(String data) {
      try {
         return SmUtil.sm4(SecureUtil.decode(properties.getSm4Key())).encryptBase64(data) + "##encrypted";
      } catch (Exception var2) {
         log.debug("SM4加密异常:{}", var2.getMessage());
         return data;
      }
   }

   public static String sm4Encryptbyconfig(String data) {
      try {
         return properties.isEncrypted() ? SmUtil.sm4(SecureUtil.decode(properties.getSm4Key())).encryptBase64(data) + "##encrypted" : data;
      } catch (Exception var2) {
         log.debug("SM4加密异常:{}", var2.getMessage());
         return data;
      }
   }

   public static String desensitizedByConfig(String data, DesensitizedUtil.DesensitizedType desensitizedType) {
      try {
         if (properties.isDesensitization()) {
            return data.endsWith("##encrypted") ? DesensitizedUtil.desensitized(sm4Decrypt(data), desensitizedType) : DesensitizedUtil.desensitized(data, desensitizedType);
         } else {
            return data;
         }
      } catch (Exception var3) {
         log.debug("脱敏异常:{}", var3.getMessage());
         return data;
      }
   }

   public static String sm4Decrypt(String data) {
      try {
         if (data.endsWith("##encrypted")) {
            data = data.substring(0, data.length() - "##encrypted".length());
            return SmUtil.sm4(SecureUtil.decode(properties.getSm4Key())).decryptStr(data);
         } else {
            return data;
         }
      } catch (Exception var2) {
         log.debug("SM4解密异常:{}", var2.getMessage());
         return data;
      }
   }

   public static void main(String[] args) {
//      System.out.println(sm4Encrypt("18110922397"));
//      System.out.println(sm4Decrypt("J1KE3IyZqIqgWk09Z2gWpQ==##encrypted"));
//
//      System.out.println(sm4Encrypt("13866134935"));
//      System.out.println(sm4Decrypt("29G1AUHDmLvPe1tt0t12Dw==##encrypted"));

      String src = decryptBySm4("eNAwpvN8VcbRXal9POGGV5X5y4Pa9oPNn8ewmFkmn9q/EQavDKPbUSpHRc9iI8Yu\n",
              Base64Decoder.decode("StqtdHhleMKClUGbmBqdrg=="));
      System.out.println(src);
      String src1 = decryptBySm4("Bkv90NIOHy70kV/ex7l6gaNx7Ir+L61MXjuTr55tXpRZtOOX8XKXqMqBAjBbGLLTlEBycUwDSHAq0Ndu6cEIOyqGLK5ClvqZjWiL/iwPnQ+IEEqA6uV4rWzFtNa9NanlpIo6kzNCXzxiQgFOxgHmI+8Sw8cAyzN7bKZ2dryLQLUD+B68YHwGnGKYuQObIebCPj/wIO1UhoTp9WZqLHog+JTF72cG4nd45JV1xsCbeV8zljplgDlzogWXwhyZHiaphu2O7Si2yfz9/7sIZNyHSF+3SW1OuBgobS5Ia3oxCsKQP+PeJt3P/iPULVJiJDssGVIdFUg9sLkkP6DluszK2T5BMInEutLwqtwwopX97uFLhisKr1LqTKODbnbMWPvJ5hL4GivwWNJVKvBqoYjN7UhupWcTvTAUsd4P0B9rECRB0zut2M2VbkmyQmZsgxoiU5mYoEaoxYunkShHeFZEv6aMpACa9M++7eOk6IAs4b4TUbTV75omnpa7H2g6mR+RKCN0oLhDmJ2LHmsJx/0nZsBmKiKZOTPpGJYJPmxVLT9SZPor2Bj7mO/KK5Hbzklbqn4Lv9l6nx3Xmrjs+LGighfMCBQQyPK2gwJDZapnhphkbTeFY50V60XVdvOZIPCiDzqAm/CkF7NuVSUSy0M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z18ZkEb3ObhWHmc3krdgjJOVmu0sSUz/orEtaxh6CgJh36BQy4tg4Zs2SncFxTTDht84kH1+UJkl3BptHEZvo7pyRVmmCoRgHXSoB+fgNj0spgt/rWkId5KtuLf4L497qoGGMv24G7dElFJq4nrQmn12c76DvELpzKveNffMX/PDCVCqF2NuVH5scEMRt/RjikICzMkh/93L6QhuP/KuBYlisxNWAg3kTp7JE4gS8hoFN6YxMRNPVMAX3zY0DDr6Z4rbObCcnIrHpQGwcofPbpIhJsPxIn7Slig7SFWR9EBfiW5F7TxrF4WVYMowcWKsBJPcn6BHcjljdQ5KFtMFjt6suJxOEjcXl/8CQewd8jZ9uEyzyE6q+sWB4t7rxt7xVOswrLBXZncXdZVi3/a4HvJ46y8ev7I6IBVcvfSEAXQyt0Jny2M5lTlBUOHPmDzU18nYPSkSQmDlbk01XfjOwk0gYgSaCaBpADFYXviw3+8FdKHz46ijLt82X3gzxQDFbEa4iBDFy+z6COprPOuHNviqirh3NGyyGDyohgAaWyGNERxhtxY5WBq21B5ULXRFoRks1ai24NKcDU3EaeorD1hrabdbQ674Mx64y6siaDv2k7wfK7B8nEbnJoVoB1Nvd/9NmCT6sW+HHoqTQGX0pyV7TJYkvK2orUcfGXd/3GQ8G2xTLKrS6TZkPGtZnk+bumWjS1XVSLILFaEc1278p7Uu+lPrnLl/6gdJRO744IUFKfHzdpNAcXcQIhg/YBEKGVy/fNa0VrrFinsmZ/aan+ZropwYqx1YOoeNwzYBSogKP3rdHLO38xcCpqxMtfTDwFdvxBEiMyjRel4HETzo2H1PAmv20ZhoFCCc7Gha/mMUgCnMiuR7EbKPj9tihE7mMPZn00hbZ+5wubdqS6A7AAOUKp2Ns3sDZC/qDm7F4aYZThYWH8mx7oWPngwM9omVLsvy/04IUH8LuJZeGp5H2Bc/MqNMAV34hDa6scIiW5iQn4=",
              Base64Decoder.decode(src));
      System.out.println(src1);

//      String src2 = decryptBySm4("Fm61oDlfUUyw1827TmMJnMym9V7KeX90sFNBSEKklDCCF+xeP6xroJ8h51DSHEkLm9oLIz3zjZ67pyHQbNr1Ewy/IOa3oS3urOcs+GQM1Bbept49M4gYVlR+9T5PVBh/oGTxWCLDFGXniZ//I3TPzTZ4d0lvYYQXdU8urbRlG4h+HwVYHupezgESM/7znsxR1cgVRybPdAiabQBasPzJdI8VKx+g+xVR3ee2hPtLZTXBf6ncvbj4f6eXzq5Ak5NnDASJEdJC9gj4pv2evfogAxZNoFLS0hPRde44sjGBXpJ3pWLWp/03iIrLlCQC2V4qugUULZMWaRXp3VzXbvHxbjqzPybMsocBdylfpPDrlj8AMJwt4c48AV+5VSSX4OM7LWwiW7N1Wi8pXbzzzUiUPwJa2T5y3rd40RG8JCruBEoD58t68vt8PoCcdbi4oyO7iY0AZsGxqXkJML9khMzb7yyGogZ6UHVNuzEMZbwbCmMrr2TNIye47yWD4MrB4Em+AwoiCPQMOcZ4uoqtJ79z4+BKWnhb3wy9/Cre54gpLnnAPlEDcmsjVjphjebp9qeGQH9xYXgI/RujjjAUMVDfEIBCHehKjbxTNEMxKesxsQJ/oK4DTOJV71OK4FmxMzdIqkNj02nzoOJC1c7S6ybJcjSP0ls0KRkQq6BLlPxldzGkT6lIUcFfd9foqqETKVDhCisSmKK1oAkdqCd4G6DKF2HFnZmjlanss6KhjdNJoi5cPxpFfVWFG7WxnswOt8FH0eMbanwaY2OuLJ0IL/AoW89iEQQ1vOFCbA2ijrPvAZ8P/YVlXKxXKrcIBHTRfDNjbNrl4VF1/5b0PZWt2BO3BUbT1hSTW5T4M1K0XAqf91e11DgUcp0wlgXE9oyCrcWAMUqwCYYdWEMtvktN3gSeKCHrmHnb+BWHZDVwHIltTbz90guY0k7GgURXSHLPIu+O",
//              Base64Decoder.decode(src));
//      System.out.println(src2);


   }

   public static String decryptBySm4(String data, byte[] key) {
      byte[] decodedData = Base64Decoder.decode(data);
      if (decodedData.length <= 16) {
         throw new IllegalArgumentException("Encrypted data byte length must greater than 16");
      } else {
         byte[] iv = PrimitiveArrayUtil.sub(decodedData, 0, 16);
         byte[] dataBytes = PrimitiveArrayUtil.sub(decodedData, 16, decodedData.length);
         return (new SM4(Mode.CBC, Padding.PKCS5Padding, key, iv)).decryptStr(dataBytes);
      }
   }
}
