content: package com.alphabugx.security;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Method;
  import java.util.concurrent.ConcurrentHashMap;
  
  /**
   * 动态保护类
   * 实现运行时解密、动态类加载等高级保护技术
   */
  public class DynamicProtection {
      
      // 运行时解密缓存
      private static final ConcurrentHashMap<String, Object> decryptionCache = new ConcurrentHashMap<>();
      
      // 动态生成的类名和方法名
      private static final String[] DYNAMIC_CLASS_NAMES = {"a", "b", "c", "d", "e", "f"};
      private static final String[] DYNAMIC_METHOD_NAMES = {"x", "y", "z", "m", "n", "p"};
      
      // 加密的关键字符串
      private static final String ENCRYPTED_CFR_PATH = "Y2ZyLTAuMTUyLmphcg=="; // base64: cfr-0.152.jar
      private static final String ENCRYPTED_JAVAC = "amF2YWMuZXhl"; // base64: javac.exe
      
      static {
          // 静态初始化时预热解密缓存
          initializeDecryptionCache();
      }
      
      /**
       * 初始化解密缓存
       */
      private static void initializeDecryptionCache() {
          try {
              // 预解密一些常用字符串
              decryptionCache.put("cfr_tool", decryptBase64(ENCRYPTED_CFR_PATH));
              decryptionCache.put("javac_tool", decryptBase64(ENCRYPTED_JAVAC));
              
              // 添加一些假的缓存项以混淆
              decryptionCache.put("fake_key_1", "fake_value_1");
              decryptionCache.put("fake_key_2", "fake_value_2");
              
          } catch (Exception e) {
              // 静默处理初始化错误
          }
      }
      
      /**
       * Base64解密
       */
      private static String decryptBase64(String encrypted) {
          try {
              return new String(java.util.Base64.getDecoder().decode(encrypted));
          } catch (Exception e) {
              return encrypted; // 解密失败时返回原文
          }
      }
      
      /**
       * 动态获取解密的字符串
       */
      public static String getDynamicString(String key) {
          // 检查缓存
          Object cached = decryptionCache.get(key);
          if (cached instanceof String) {
              return (String) cached;
          }
          
          // 动态解密
          return performDynamicDecryption(key);
      }
      
      /**
       * 执行动态解密
       */
      private static String performDynamicDecryption(String key) {
          try {
              // 使用反射和动态计算增加分析难度
              Class<?> stringClass = String.class;
              Method valueOfMethod = stringClass.getMethod("valueOf", Object.class);
              
              // 复杂的计算过程
              StringBuilder builder = new StringBuilder();
              for (int i = 0; i < key.length(); i++) {
                  char c = key.charAt(i);
                  builder.append((String) valueOfMethod.invoke(null, (char)(c ^ 0x33))); // 简单XOR
              }
              
              String result = builder.toString();
              decryptionCache.put(key, result); // 缓存结果
              return result;
              
          } catch (Exception e) {
              return key; // 失败时返回原始key
          }
      }
      
      /**
       * 动态类加载器
       */
      public static Class<?> loadDynamicClass(String className) {
          try {
              // 动态构造真实类名
              String realClassName = obfuscateClassName(className);
              
              // 使用反射加载类
              return Class.forName(realClassName);
              
          } catch (Exception e) {
              return null;
          }
      }
      
      /**
       * 混淆类名
       */
      private static String obfuscateClassName(String input) {
          // 通过复杂的字符串操作隐藏真实类名
          StringBuilder result = new StringBuilder();
          
          // 假的混淆过程
          for (String dynamicName : DYNAMIC_CLASS_NAMES) {
              if (input.contains(dynamicName)) {
                  result.append(input.replace(dynamicName, "real"));
                  break;
              }
          }
          
          return result.length() > 0 ? result.toString() : input;
      }
      
      /**
       * 动态方法调用
       */
      public static Object invokeDynamicMethod(Object target, String methodName, Object... args) {
          try {
              Class<?> targetClass = target.getClass();
              Method[] methods = targetClass.getDeclaredMethods();
              
              // 查找匹配的方法
              for (Method method : methods) {
                  if (method.getName().equals(methodName)) {
                      method.setAccessible(true);
                      return method.invoke(target, args);
                  }
              }
              
              return null;
          } catch (Exception e) {
              return null;
          }
      }
      
      /**
       * 创建动态代理对象
       */
      public static Object createDynamicProxy(String className, Object... constructorArgs) {
          try {
              Class<?> clazz = loadDynamicClass(className);
              if (clazz == null) {
                  return null;
              }
              
              // 查找合适的构造函数
              Constructor<?>[] constructors = clazz.getConstructors();
              for (Constructor<?> constructor : constructors) {
                  if (constructor.getParameterCount() == constructorArgs.length) {
                      return constructor.newInstance(constructorArgs);
                  }
              }
              
              // 如果没有找到匹配的构造函数，使用默认构造函数
              return clazz.newInstance();
              
          } catch (Exception e) {
              return null;
          }
      }
      
      /**
       * 运行时验证码生成
       */
      public static String generateRuntimeVerificationCode() {
          try {
              // 基于当前时间和系统属性生成验证码
              long timestamp = System.currentTimeMillis();
              String osName = System.getProperty("os.name", "unknown");
              String javaVersion = System.getProperty("java.version", "unknown");
              
              // 复杂的哈希计算
              int hash = (osName + javaVersion + timestamp).hashCode();
              hash ^= 0xDEADBEEF; // 固定异或值
              
              return Integer.toHexString(Math.abs(hash));
              
          } catch (Exception e) {
              return "default_verification_code";
          }
      }
      
      /**
       * 验证运行时验证码
       */
      public static boolean verifyRuntimeCode(String code) {
          try {
              String expectedCode = generateRuntimeVerificationCode();
              return code != null && code.equals(expectedCode);
          } catch (Exception e) {
              return false;
          }
      }
      
      /**
       * 清理动态缓存
       */
      public static void clearDynamicCache() {
          try {
              decryptionCache.clear();
              
              // 重新初始化基本缓存
              initializeDecryptionCache();
              
          } catch (Exception e) {
              // 静默处理清理错误
          }
      }
      
      /**
       * 获取动态方法名
       */
      public static String getDynamicMethodName(int index) {
          if (index >= 0 && index < DYNAMIC_METHOD_NAMES.length) {
              return DYNAMIC_METHOD_NAMES[index];
          }
          return "unknownMethod";
      }
      
      /**
       * 执行混淆验证
       */
      public static boolean performObfuscatedValidation() {
          try {
              // 多层验证逻辑
              boolean step1 = AntiDecompileShield.isProtectionActive();
              boolean step2 = SecurityManager.isSecureEnvironment();
              boolean step3 = verifyRuntimeCode(generateRuntimeVerificationCode());
              
              // 通过位运算隐藏真实的验证逻辑
              int result = (step1 ? 1 : 0) | (step2 ? 2 : 0) | (step3 ? 4 : 0);
              
              // 只有当所有验证都通过时才返回true
              return result == 7; // 111 in binary
              
          } catch (Exception e) {
              return false;
          }
      }
  } 