Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class XORPatternFinder {
- private static final byte[] constantArray = {
- (byte) 0x1F, (byte) 0xF9, (byte) 0xF5, (byte) 0x99, (byte) 0xAC, (byte) 0x9B, (byte) 0x58, (byte) 0x7B,
- (byte) 0x9F, (byte) 0x65, (byte) 0xE0, (byte) 0x7D, (byte) 0xAA, (byte) 0x3F, (byte) 0xCD, (byte) 0xB9,
- (byte) 0x49, (byte) 0xD7, (byte) 0xB2, (byte) 0xCE, (byte) 0xAA, (byte) 0x8F, (byte) 0x0C, (byte) 0x75,
- (byte) 0x81, (byte) 0x73, (byte) 0xB5, (byte) 0x51, (byte) 0xF3, (byte) 0x6E, (byte) 0x98, (byte) 0x80,
- (byte) 0x52, (byte) 0x82, (byte) 0xE2, (byte) 0x8D, (byte) 0xAB, (byte) 0xB3, (byte) 0x4D, (byte) 0x66,
- (byte) 0x99, (byte) 0x7C, (byte) 0xB6, (byte) 0x7C, (byte) 0xFA, (byte) 0x6F, (byte) 0x83, (byte) 0xBC,
- (byte) 0x4D, (byte) 0xCB, (byte) 0xD5
- };
- public static void main(String[] args) {
- findValidCandidates();
- }
- private static void findValidCandidates() {
- System.out.println("Valid candidates for Array1:");
- for (int pos = 0; pos < 8; pos++) {
- System.out.printf("\nPosition %d (XORs with indices %s):\n", pos,
- getAffectedIndices(pos));
- findCandidatesForPosition(pos);
- }
- System.out.println("\nValid candidates for Array2:");
- for (int pos = 0; pos < 8; pos++) {
- System.out.printf("\nPosition %d (XORs with indices %s):\n", pos,
- getAffectedIndices(pos + 8));
- findCandidatesForPosition(pos + 8);
- }
- }
- private static void findCandidatesForPosition(int pos) {
- StringBuilder validBytes = new StringBuilder();
- StringBuilder resultingChars = new StringBuilder();
- for (int b = 0; b < 256; b++) {
- byte candidate = (byte) b;
- if (isValidByte(candidate, pos)) {
- if (validBytes.length() > 0) {
- validBytes.append(", ");
- resultingChars.append(", ");
- }
- validBytes.append(String.format("0x%02X", candidate));
- resultingChars.append(getResultingChars(candidate, pos));
- }
- }
- System.out.println("Bytes: " + validBytes);
- System.out.println("Chars: " + resultingChars);
- }
- private static String getResultingChars(byte candidate, int startPos) {
- StringBuilder chars = new StringBuilder();
- for (int i = startPos; i < constantArray.length; i += 16) {
- chars.append((char)(constantArray[i] ^ candidate));
- }
- return chars.toString();
- }
- private static String getAffectedIndices(int startPos) {
- StringBuilder indices = new StringBuilder();
- for (int i = startPos; i < constantArray.length; i += 16) {
- if (indices.length() > 0) indices.append(", ");
- indices.append(i);
- }
- return indices.toString();
- }
- private static boolean isValidByte(byte candidate, int startPos) {
- for (int i = startPos; i < constantArray.length; i += 16) {
- int result = constantArray[i] ^ candidate;
- if (!isAllowedChar((char)result)) {
- return false;
- }
- }
- return true;
- }
- private static boolean isAllowedChar(char c) {
- return (c >= 'A' && c <= 'Z') ||
- (c >= 'a' && c <= 'z') ||
- (c >= '0' && c <= '9') ||
- c == '{' || c == '}';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement