# Smart approach or Brute force?

Background: I'm a beginner reverse engineer and I wanted to try writing my own c++ programs and reverse-engineering them. I wrote something and I'm not sure a smart way of reversing it. Usually, I can think of it as a math formula and just do it in reverse to figure out the algorithm but I can't think of any other way than brute-forcing for this scenario.

``````#include <iostream>

using namespace std;

int main()
{
char key[9];
int total = 0;

cout << "Enter Key: ";

cin >> key;

for (char item : key)
{
total = total + (int)item;
}

if (total == 895)
{
puts("Correct");
}
else
{
puts("Sorry buddy");
}

return 0;
}
``````

btw sorry if my c++ isn't very good. I am new to this language haha

Main Goal: I want to know if there is a smarter way of going about this or is brute force the only way. Thanks!

Your code basically takes 9 chars, 8 from user input + 1 null terminator. each one can be between 0-255.

You char array bytes are signed, so maximum value for each is 127 (hex: 7F)

You want the result of those byte values to end up equal to 895.

In order to achieve the result, you need to pass any combination of bytes that sum up to your value.

Example: 127*7+6 = 895

hex: \x7F\x7F\x7F\x7F\x7F\x7F\x7F\x06

`echo 7f7f7f7f7f7f7f06 | xxd -r -p|./compiled_executable`

`xxd -r -p` simply takes hex values and converts them to chars.

Note that the max value your input can end up with is 1016. More than that - nothing will solve it and it will always fail.