|
| 1 | +package dev.skidfuscator.obfuscator.transform.impl.string.generator.algo; |
| 2 | + |
| 3 | +import dev.skidfuscator.obfuscator.event.EventBus; |
| 4 | +import dev.skidfuscator.obfuscator.event.impl.transform.method.InitMethodTransformEvent; |
| 5 | +import dev.skidfuscator.obfuscator.skidasm.SkidClassNode; |
| 6 | +import dev.skidfuscator.obfuscator.skidasm.SkidMethodNode; |
| 7 | +import dev.skidfuscator.obfuscator.transform.impl.string.generator.EncryptionGenerator; |
| 8 | +import org.objectweb.asm.Label; |
| 9 | +import org.objectweb.asm.MethodVisitor; |
| 10 | +import org.objectweb.asm.Opcodes; |
| 11 | + |
| 12 | +import javax.crypto.Cipher; |
| 13 | +import javax.crypto.spec.IvParameterSpec; |
| 14 | +import javax.crypto.spec.SecretKeySpec; |
| 15 | +import java.nio.ByteBuffer; |
| 16 | +import java.nio.CharBuffer; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.Random; |
| 19 | + |
| 20 | +import static org.objectweb.asm.Opcodes.*; |
| 21 | + |
| 22 | +public class IvEncryptionGenerator implements EncryptionGenerator { |
| 23 | + private static final String KEY = "ComplexKey12345"; // Static key |
| 24 | + private static final String IV = "ComplexIV1234567"; // Static IV |
| 25 | + |
| 26 | + public IvEncryptionGenerator(String iv) { |
| 27 | + assert iv.length() == 16 : "AES IV must be 16 bytes long"; |
| 28 | + } |
| 29 | + |
| 30 | + private static String transform(String input, boolean encrypt) { |
| 31 | + final int prime = 257; // Use a prime number for calculations |
| 32 | + int[] keyExpansion = new int[input.length()]; |
| 33 | + int[] ivExpansion = new int[input.length()]; |
| 34 | + for (int i = 0; i < input.length(); i++) { |
| 35 | + keyExpansion[i] = (KEY.charAt(i % KEY.length()) * prime) % 256; |
| 36 | + ivExpansion[i] = (IV.charAt(i % IV.length()) * prime) % 256; |
| 37 | + } |
| 38 | + |
| 39 | + StringBuilder output = new StringBuilder(); |
| 40 | + int previousChar = 0; |
| 41 | + for (int i = 0; i < input.length(); i++) { |
| 42 | + int inputChar = input.charAt(i); |
| 43 | + int keyChar = keyExpansion[i]; |
| 44 | + int ivChar = ivExpansion[i]; |
| 45 | + int mix = (keyChar + ivChar + previousChar) % 256; // Mix with previous char for more complexity |
| 46 | + if (encrypt) { |
| 47 | + inputChar = (inputChar + mix) % 256; |
| 48 | + } else { |
| 49 | + inputChar = (inputChar - mix + 256) % 256; // Ensure positive by adding 256 before mod |
| 50 | + } |
| 51 | + previousChar = inputChar; // Use current char as previous for next iteration |
| 52 | + char outputChar = (char) inputChar; |
| 53 | + output.append(outputChar); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + // Further scramble the output using a simple transposition based on key and IV length for more complexity |
| 58 | + if (encrypt) { |
| 59 | + output = new StringBuilder(scramble(output.toString(), KEY.length() + IV.length())); |
| 60 | + } else { |
| 61 | + output = new StringBuilder(unscramble(output.toString(), KEY.length() + IV.length())); |
| 62 | + } |
| 63 | + |
| 64 | + return output.toString(); |
| 65 | + } |
| 66 | + |
| 67 | + private static String scramble(String input, int step) { |
| 68 | + char[] arr = new char[input.length()]; |
| 69 | + for (int i = 0; i < input.length(); i++) { |
| 70 | + int j = (i * step) % input.length(); |
| 71 | + arr[j] = input.charAt(i); |
| 72 | + } |
| 73 | + return new String(arr); |
| 74 | + } |
| 75 | + |
| 76 | + private static String unscramble(String input, int step) { |
| 77 | + char[] arr = new char[input.length()]; |
| 78 | + for (int i = 0; i < input.length(); i++) { |
| 79 | + int j = (i * step) % input.length(); |
| 80 | + arr[i] = input.charAt(j); |
| 81 | + } |
| 82 | + return new String(arr); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public byte[] encrypt(String input, int key) { |
| 87 | + try { |
| 88 | + return transform(input, true).getBytes(StandardCharsets.UTF_8); |
| 89 | + } catch (Throwable ex) { |
| 90 | + throw new IllegalStateException("Failed to encrypt", ex); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String decrypt(byte[] input, int key) { |
| 96 | + try { |
| 97 | + return transform(new String(input, StandardCharsets.UTF_8), false); |
| 98 | + } catch (Throwable ex) { |
| 99 | + throw new IllegalStateException("Failed to decrypt", ex); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void visit(final SkidClassNode node, String name) { |
| 105 | + |
| 106 | + } |
| 107 | +} |
0 commit comments