package com.fdkankan.reg;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
/***
* @description:激光相机本地版加密解密SNcode配合laserGen项目使用
* @author: Xiewj
* @date: 2023/6/14 18:19
* @param:
* @return:
**/
public class Crypt {
   private static String key = "4dage1684dage168";


   //用于构建十六进制输出
   private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

   /**
    * AES解密 128 AES/ECB/PKCS5Padding
    *
    * @param source      需要解密的字符串
    * @param hexOrBase64 true:hex,false:base64
    * @return 返回解密结果
    */
   public static String decryptAES(String source, boolean hexOrBase64) {
      try {
         SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");
         Cipher cipher = Cipher.getInstance("AES");
         cipher.init(Cipher.DECRYPT_MODE, secretKey);
         if (hexOrBase64) {
            return new String(cipher.doFinal (hex2bytes(source))).toUpperCase();
         } else {
            return new String(cipher.doFinal( decodeBase64(source))).toUpperCase();
         }
      } catch (Exception e) {
         System.out.println(e.getMessage());
         throw new RuntimeException("出错啦!", e);
      }
   }
   /**
    * AES加密 128 AES/ECB/PKCS5Padding
    *
    * @param source      需要加密的字符串
    * @param hexOrBase64 true:hex,false:base64
    * @return 返回加密结果
    */
   public static String encryptAES(String source,   boolean hexOrBase64) {
      try {
         SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");
         Cipher cipher = Cipher.getInstance("AES");
         cipher.init(Cipher.ENCRYPT_MODE, secretKey);
         if (hexOrBase64) {
            return new String( bytes2hex(cipher.doFinal(source.getBytes()))).toUpperCase();
         } else {
            return  encodeBase64(cipher.doFinal(source.getBytes())).toUpperCase();
         }
      } catch (Exception e) {
         throw new RuntimeException("出错啦!", e);
      }

   }

   public static byte[] hex2bytes(String hexStr) {
      byte[] bytes = new byte[hexStr.length() / 2];
      for (int i = 0; i < hexStr.length(); i += 2) {
         bytes[i / 2] = (byte) ((Character.digit(hexStr.charAt(i), 16) << 4)
                 + Character.digit(hexStr.charAt(i+1), 16));
      }
      return bytes;
   }


   public static byte[] decodeBase64(String text) {
      return java.util.Base64.getDecoder().decode(text);
   }


   public static char[] bytes2hex(final byte[] data) {
      final int l = data.length;
      final char[] out = new char[l << 1];
      // two characters form the hex value.
      for (int i = 0, j = 0; i < l; i++) {
         out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
         out[j++] = DIGITS_LOWER[0x0F & data[i]];
      }
      return out;
   }


   public static String encodeBase64(byte[] data) {
      return Base64.getEncoder().encodeToString(data);
   }



   public static void main(String[] args) throws Exception {
      String value ="8F7DEOBF155D172C89EF5F4C628A1983F822E82AOCA1EE1ED764DAAD04C7D96FC2EFECE222ECC9F5COD5845830FEEE98";
      //"HmtaYAN+n8dzKifi50/sA4Sv/dLKR5MnfZKcJVSzmST0D7ZkbmM8AebuNNkKBc4Z7DoXDnDwwwap2hg+6ztWQAZdQZOfAHR5bueqpApDN9il6gIuQadcdPcvrurRRiTifOQYsE9nRQKy6smol0jrz0wLEDxyPh3ZfBZG0e5KxrEq6Es6E3/05F4jIBYy+dUtvYBPSrVi3kzkblFex6qZOf/6jbtSlchWJVXMuEgA05LplUlUvNr5vwNC65KIy6beHcyhntIO2wJHx5uknjtidXUft3s87ZlxTzMEiKV6cXU9ejuCxdOz9n0c7UPeAOqHprxW2FvOhytVSMoSPP6No9pSochmKdXBPgOQd15rswBi0ejSZ+qWG4/MrBhVFD5OaQ8Cr7wy9hoWdnYrd0tcxPZv4EiwF3DDqctffYgQjy8=";
      //"openid=ouQdHv18KY5HPhW16cDk43Y5-yyk&nickname=%e6%9d%8e%e5%a8%81&headimg=http%3a%2f%2fthirdwx.qlogo.cn%2fmmopen%2fajNVdqHZLLB6qkUjQibrUJniaBhLf8hjS5L4roMR84T62m7NyHiaIAXKh1cl2vJwfdSfEzju8HicugEXbJQFHx1pZw%2f132&rytype=1&nwflag=1&empno=BEJY3293&stime=1603340863000";
      String key = "4dage1684dage168";

//      //加密
      String s = encryptAES(value, true);
      System.out.println(s);
      //解密
      String k = decryptAES("9013E47C4A1181E70B697E956F7D8F16E776FB0F0D14FEF35C31158D889EAED9C0AACA53E67FE3F54468DE6F90B123E24E663E0AED062C1258A2CF937B17451958D115C4DDC7C317E1E06C174B748570F8618BBFD7A5AF56CFDDF48E2112BBD5033FBF152BB9966B8489EDD346201562", true);
      System.out.println(k);

   }

}