Advertisement
Sdrago

Untitled

Jan 22nd, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. public class XORPatternFinder {
  2.     private static final byte[] constantArray = {
  3.         (byte) 0x1F, (byte) 0xF9, (byte) 0xF5, (byte) 0x99, (byte) 0xAC, (byte) 0x9B, (byte) 0x58, (byte) 0x7B,
  4.         (byte) 0x9F, (byte) 0x65, (byte) 0xE0, (byte) 0x7D, (byte) 0xAA, (byte) 0x3F, (byte) 0xCD, (byte) 0xB9,
  5.         (byte) 0x49, (byte) 0xD7, (byte) 0xB2, (byte) 0xCE, (byte) 0xAA, (byte) 0x8F, (byte) 0x0C, (byte) 0x75,
  6.         (byte) 0x81, (byte) 0x73, (byte) 0xB5, (byte) 0x51, (byte) 0xF3, (byte) 0x6E, (byte) 0x98, (byte) 0x80,
  7.         (byte) 0x52, (byte) 0x82, (byte) 0xE2, (byte) 0x8D, (byte) 0xAB, (byte) 0xB3, (byte) 0x4D, (byte) 0x66,
  8.         (byte) 0x99, (byte) 0x7C, (byte) 0xB6, (byte) 0x7C, (byte) 0xFA, (byte) 0x6F, (byte) 0x83, (byte) 0xBC,
  9.         (byte) 0x4D, (byte) 0xCB, (byte) 0xD5
  10.     };
  11.     public static void main(String[] args) {
  12.         findValidCandidates();
  13.     }
  14.     private static void findValidCandidates() {
  15.         System.out.println("Valid candidates for Array1:");
  16.         for (int pos = 0; pos < 8; pos++) {
  17.             System.out.printf("\nPosition %d (XORs with indices %s):\n", pos,
  18.                             getAffectedIndices(pos));
  19.             findCandidatesForPosition(pos);
  20.         }
  21.         System.out.println("\nValid candidates for Array2:");
  22.         for (int pos = 0; pos < 8; pos++) {
  23.             System.out.printf("\nPosition %d (XORs with indices %s):\n", pos,
  24.                             getAffectedIndices(pos + 8));
  25.             findCandidatesForPosition(pos + 8);
  26.         }
  27.     }
  28.     private static void findCandidatesForPosition(int pos) {
  29.         StringBuilder validBytes = new StringBuilder();
  30.         StringBuilder resultingChars = new StringBuilder();
  31.        
  32.         for (int b = 0; b < 256; b++) {
  33.             byte candidate = (byte) b;
  34.             if (isValidByte(candidate, pos)) {
  35.                 if (validBytes.length() > 0) {
  36.                     validBytes.append(", ");
  37.                     resultingChars.append(", ");
  38.                 }
  39.                 validBytes.append(String.format("0x%02X", candidate));
  40.                 resultingChars.append(getResultingChars(candidate, pos));
  41.             }
  42.         }
  43.        
  44.         System.out.println("Bytes: " + validBytes);
  45.         System.out.println("Chars: " + resultingChars);
  46.     }
  47.     private static String getResultingChars(byte candidate, int startPos) {
  48.         StringBuilder chars = new StringBuilder();
  49.         for (int i = startPos; i < constantArray.length; i += 16) {
  50.             chars.append((char)(constantArray[i] ^ candidate));
  51.         }
  52.         return chars.toString();
  53.     }
  54.     private static String getAffectedIndices(int startPos) {
  55.         StringBuilder indices = new StringBuilder();
  56.         for (int i = startPos; i < constantArray.length; i += 16) {
  57.             if (indices.length() > 0) indices.append(", ");
  58.             indices.append(i);
  59.         }
  60.         return indices.toString();
  61.     }
  62.     private static boolean isValidByte(byte candidate, int startPos) {
  63.         for (int i = startPos; i < constantArray.length; i += 16) {
  64.             int result = constantArray[i] ^ candidate;
  65.             if (!isAllowedChar((char)result)) {
  66.                 return false;
  67.             }
  68.         }
  69.         return true;
  70.     }
  71.     private static boolean isAllowedChar(char c) {
  72.         return (c >= 'A' && c <= 'Z') ||
  73.                (c >= 'a' && c <= 'z') ||
  74.                (c >= '0' && c <= '9') ||
  75.                c == '{' || c == '}';
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement