1

This is my first ever question on StackOverflow so if I do something wrong don't go too hard on me. Anyways, I have found a certain encryption in a C file I was looking in and I have the decryption code but I have no clue on how to reverse it so I can encrypt my own string into it.

    int r;
    char str[] = {"4:;145;14;81583"};
    for(r = 0; (r < 100 && str[r] != '\0'); r++)
    str[r] = str[r] - 3;

The encrypted string (4:;145;14;81583) is the IP 178.128.185.250 Like I said, I am wondering how to reverse the decryption method so that I can encrypt my own strings. Help?

3
  • That solved it. That was so simple. Commented Aug 3, 2018 at 22:53
  • This was the part that confused me. for(r = 0; (r < 100 && str[r] != '\0'); r++) Commented Aug 3, 2018 at 23:02
  • It just means: Continue the loop until end of string (i.e. str[r] != '\0') or at maximum 100 chars (i.e. r < 100) I have no idea why the max of 100 chars is added. Seems like pure nonsense Commented Aug 3, 2018 at 23:04

1 Answer 1

2

It's straight forward: Decrypt is - 3 so encrypt is + 3

Decrypt

#include <stdio.h>

int main(void) {
    int r;
    char str[] = {"4:;145;14;81583"};
    for(r = 0; (r < 100 && str[r] != '\0'); r++)
        str[r] = str[r] - 3;
                //      ^ notice: minus
    printf("%s\n", str);
    return 0;
}

Output:

178.128.185.250

Encrypt

include <stdio.h>

int main(void) {
    int r;
    char str[] = {"178.128.185.250"};
    for(r = 0; (r < 100 && str[r] != '\0'); r++)
        str[r] = str[r] + 3;
                //      ^ notice: plus
    printf("%s\n", str);
    return 0;
}

Output:

4:;145;14;81583

Edit:

The line

for(r = 0; (r < 100 && str[r] != '\0'); r++)

is copied directly from OPs question. As far as I can see, it can be simplified to:

for(r = 0; str[r] != '\0'; r++)

without any problems.

Sign up to request clarification or add additional context in comments.

2 Comments

I am puzzling over the r < 100. Could you explain?
@Yunnosch Well, I simply copied it directly from the question and didn't wanted to change it. But you're correct that it seems strange so I added some extra text to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.