Home > Blockchain >  Solving for a Variable
Solving for a Variable

Time:02-02

How would I got about solving for the variable num, which would be my own input to the program, in the compare function?

main function (part)

int input;
answer = compare(input);

if(answer !=0xb88202) {
   printf("Keep trying!");
   exit(0);
}

puts("You got it");

compare function

uint compare(uint num) {
     return (num ^ 0x735) >> 4 ^ 0x6f0;
}

I'm honestly not sure how to go about this. I figured somehow reversing the XOR with another XOR would help. I also tried left shifting 0xb88202 by the 4 ^ 0x6f0 but I just can't figure this out.

CodePudding user response:

Just reverse the operations in the compare function.

input = (0xb88202 ^ 0x6f0) << 4 ^ 0x735

CodePudding user response:

Treat it as an equation and solve. You have

answer == ((num ^ 0x735) >> 4) ^ 0x6f0

xor is its own inverse, so you get

answer ^ 0x6f0 == (num ^ 0x735) >> 4

(answer ^ 0x6f0) << 4 == (num ^ 0x735)

((answer ^ 0x6f0) << 4) ^ 0x735 == num

This isn't quite correct for the shift as a right shift loses bits (shifted off the bottom) that the left shift won't recover, but this just means that there are multiple values of num that will give you that answer and this finds one of them.

  •  Tags:  
  • c
  • Related