package com.bonus.common.houqin.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 com.bonus.common.core.utils.encryption.Sm4Utils;
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 data = Sm4Utils.encrypt(data);
      } catch (Exception var2) {
         log.debug("SM4加密异常:{}", var2.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 {
            data = Sm4Utils.decrypt(data);
            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("GKwQwyM8ltMF7hDQo0VlIw==##encrypted"));
//
//      System.out.println(sm4Encrypt("13866134935"));
//      System.out.println(sm4Decrypt("29G1AUHDmLvPe1tt0t12Dw==##encrypted"));

      String src = decryptBySm4("4z487AdShRtt4UB3HOhEdoaC5j5C3ssXfI+T4UHDV3Ne0tiWlfT9VgQjyMY2bz4J\n",
              Base64Decoder.decode("StqtdHhleMKClUGbmBqdrg=="));
      System.out.println(src);
      String src1 = decryptBySm4("miU+rUiYcrDot9bCuiISB1Xepg7ROsxyVDx9FfcO8L5uGZ7ixXPhAAbTj1Rp5qAB9w70qiCWhO4Gzx/hz2kRgsbnbvBeVEPRNRonRYJH4oPv2kC4v8Ha4tKbpy8WMNUyx4fqKRmX4wArqnwOkOQ1NEM/+cD10BmUUWvsbH/DJqTnrEQasrKmN0l/3Y49OfcJ+e0dDtCtJ2hEC+88/jJHFhSEtovTUrQcqfhffZuB+gJCO2r68s7fQRei+Un1S61RmZLsfq2LTjLBKj7yckklCcrEKqso6mza5Maj65ZYmZnx9w5Z0v0WEE8vN5JaEAlQybTpfjVj041cG2BeKlfGvIgNuul2pUUdsfMky30xLwwoOpZkNZ6KBr4/6Wum1yl3g920HUbK3MaGXgSx6Mht/PWxEU1SSrM9YDhx+bvLZ/lUGHDAl0J0sPfkT5xC7LCGo1NicR0bDwtj258fW4a27HnT1b5NtMCQqYTNIDw0MPydaJrfnbY4qzEfWsnX5tWasKwwFKacZB4IJesYw6icwRKKKEwtYUz6/Z4RzZvHlh2azfKhTm+Y4cB0zsUYJXzsZRvnAQUgdgw9qLL7NvwJEMIyrcmUUwmFiZ8MV3urq6kaaCAoZoTr07OgR4J2gFisd86szZhkoX+cQJBur84dsx18rPS+4KnnysVPaWqutlQsyrEaehq2niFGbPX4FOPePAuz1Zon5U4eOOXZ9eKSbRMAtLnYjFFJ0CxsfqNlfupE5eFuKnK4cMq7BD7xEaM8dfjtlrPwr9QBTbFO7KUWYlyf1qMcqVJQUH6T6KLmft1/tObBWbeB7qHg5kq7d+CO3pXIxNUStUswhUQcbLepzfERrkJRrhAszuUitBI+vA4/AgGZOcjKCZ67ry9VgqulqO4YIbMTVw/Km6shtsGtcGpnWDUX/GH0DRxhf/xW6cD/af9PEyiMEP338g5C0gH7jl3w53aDTagoDmRF79BTHdPw8R3CIVfHD7mAEf2nD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sV6e5gDNyz5RIcITI+gMKX92YPzxdz5te9CEZFemdq4fCi5/iqvw+wFwXX+Xz0vrKhktAC3VNbrgGIFFedYXxqoTG0c4iLs1+pVVCsabRUmUzpp+OHuoSrGvK2KdqYf1z6Sutq5Nrd1ptwZq0CJei/tc48tLVPuCB/ZHjLyICl68MHjYHe/Z+e/eoiHJa6OJTOouMJvrJj/mziM7PRH2J46u1cgEzoQZpQLJx3jRB0iLChlL7iWJAVgVkohU4xbGv54if5Q1nTguySSVxzwLeLRSdqlejDGzlNa9jiiiY4FjiIX0aHp0vSUu+8B6paGAR2U/xKk=",
              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);
      }
   }
}
