I'm working on a problem whereby I am trying to take a string which has been encrypted using a known algorithm and key to it's original plaintext value.
The algorithm uses an XOR operating and bit shifting to encrypt the plaintext.
I have been working on the assumption which appears partially correct, that this process can be reversed by performing a bit shift in the opposite direction and then an XOR operation, as below.
WHERE
c = a ^ b
THEN
a = c ^ b
The encryption function generates a key via rand() which is seeded by a known value, the result of time(NULL). Given the known seed for rand() I can regenerate the keys.
This generally works fine and I am able to generate some of the correct characters from the encrypted input. You can see in the screenshot below some of the original string, "ABCDEFGHIJKLMNO" being decoded correctly.
The problem I have (I think) is the below line within the encryption function.
input[pos] = input[pos] << key | input[pos] >> 8 - key;
My understanding is the bitwise OR within C is lossy and cannot be reversed or original inputs recovered. I am slightly stumped on how to overcome this issue without creating a new script to simply brute force each position by encrypting all possible characters and looking for a match.
This is an example of what I am doing with an encryption and decryption function. Note that the encryption function is what comes with the exercise and I don't have the option of modifying it.
How can I overcome this or is it simply not possible?
#include <stdio.h>
#include <stdint.h>
void decrypt(int pos);
void encrypt(int pos);
char input[] = "ABCDEFGHIJKLMNO";
int main() {
long input_len = sizeof(input);
// Hardcoding for consistency when testing
// int seed = time(NULL);
int seed = 1663606418;
srand(seed);
for(int i = 0; i < input_len; i++) {
encrypt(i);
}
printf(input);
printf("\n\n");
// Reseeding to get the same order for rand() when decrypting
srand(seed);
for(int i = 0; i < input_len; i++) {
decrypt(i);
}
printf(input);
}
void encrypt(int pos) {
int r1 = rand();
int key = rand() & 7;
input[pos] = input[pos] ^ r1;
input[pos] = (input[pos] << key) | (input[pos] >> 8 - key);
}
void decrypt(int pos) {
int r1 = rand();
int key = rand() & 7;
input[pos] = (input[pos] >> key) | (input[pos] << 8 - key);
input[pos] = input[pos] ^ r1;
}
