Reverse Code C++

So I have this code in C++ and I have a crypted picture and the content but I only know the way it is crypted trough this code, I need to reverse it in order to get the content in bytes of the picture but i don't know how, please help me.
This is in main, the buffers are of bytes.
bytesin = number of bytes of image
nrBytesOut= number of bytes in crypted image
bufferIN = the content of the image
bufferOut= the content of the crypted image

for (int contor = 0; contor < 256; contor++)
{
DWORD retvalue = 0;
for (int i = 0; i < bytesIn; i++) {
if (bufferIn[i] == contor) {
retvalue++;
}
}
if (retvalue == 0) { continue; }
CreateBufferOut(nrBytesOut, contor);
CreateBufferOut(nrBytesOut, retValue);
var18 = 0xffffffff;

for (int contor2 = 0; contor2 < bytesIn; contor2++)
{
if (contor != bufferIn[contor2])
{
continue;
}
if (var18 == 0xffffffff)
{
CreateBufferOut(nrBytesOut, contor2);
}
else
{
CreateBufferOut(nrBytesOut, contor2 - var18);
}
var18 = contor2;
}
}


void CreateBufferOut(DWORD nrBytesOut, DWORD value) {
if (value <= 255)
{
bufferOut[nrBytesOut++] == (byte)(value && 0xFF);
}
else if (value <= 65279)
{
bufferOut[nrBytesOut++] = 0xFF;
bufferOut[nrBytesOut++] = (byte)(value >> 8) & 0xFF;
bufferOut[nrBytesOut++] = (byte)(value & 0xFF);
}
else {
bufferOut[nrBytesOut++] = 0xFF;
bufferOut[nrBytesOut++] = 0xFF;
bufferOut[nrBytesOut] = value;
nrBytesOut += 4;//value is 4 bytes long
}
}
You've posted code fragments ... there isn't enough context to warrant spending time on it.
Please use code tags when you post. Type [code] your code [/code] to generate something like the below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int contor = 0; contor < 256; contor++)
{
    DWORD retvalue = 0;
    for (int i = 0; i < bytesIn; i++) {
        if (bufferIn[i] == contor) {
            retvalue++;
        }
    }

    if (retvalue == 0) { continue; }
    CreateBufferOut(nrBytesOut, contor);
    CreateBufferOut(nrBytesOut, retValue);

    // etc.
}


Without the code tags, I completely misunderstood what your code was doing. I actually had typed up a response that was meaningless. Code tags would have made it easier to read and understand your code so that comments are meaningful.
It hurts to know that my humble article isn't more famous :,(
You should show a complete program.

Where did you get this code? It looks like it has errors in it. For example, this

1
2
        bufferOut[nrBytesOut] = value;
        nrBytesOut += 4; //value is 4 bytes long 

suggests that the programmer expects all 4 bytes of value to be copied into bufferOut, but that won't happen. Only the last byte will be stored.

Some other things about the code don't make much sense either. E.g., the way var18 is handled is strange. And nrBytesOut is almost certainly being handled incorrectly. It looks as if the programmer expects it to constantly be incremented so that new bytes will always be appended to bufferOut, but since it's passed in by value it will not retain it's updated value between calls.

Here's your code in code tags (with a couple of added comments indicated by ////).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
bytesIn    = number of bytes of image
bufferIn   = the content of the image
bufferOut  = the content of the crypted image  //// This must be global
nrBytesOut = number of bytes in crypted image

//// Count through all possible byte values
for (int counter = 0; counter < 256; counter++) {

    //// Count how many bytes are equal to counter
    DWORD retvalue = 0;
    for (int i = 0; i < bytesIn; i++)
        if (bufferIn[i] == counter)
            retvalue++;
    if (retvalue == 0)
        continue;

    CreateBufferOut(nrBytesOut, counter);
    CreateBufferOut(nrBytesOut, retValue);
    var18 = 0xffffffff;

    for (int counter2 = 0; counter2 < bytesIn; counter2++) {
        if (counter != bufferIn[counter2])
            continue;
        if (var18 == 0xffffffff)
            CreateBufferOut(nrBytesOut, counter2);
        else
            CreateBufferOut(nrBytesOut, counter2 - var18);
        var18 = counter2;
    }
}

void CreateBufferOut(DWORD nrBytesOut, DWORD value) {
    if (value <= 255) 
        bufferOut[nrBytesOut++] == (byte)(value && 0xFF);
    else if (value <= 65279) {  //// 65279 = 2**16 - 1 - 256
        bufferOut[nrBytesOut++] = 0xFF;
        bufferOut[nrBytesOut++] = (byte)(value >> 8) & 0xFF;
        bufferOut[nrBytesOut++] = (byte)(value & 0xFF);
    }
    else {
        bufferOut[nrBytesOut++] = 0xFF;
        bufferOut[nrBytesOut++] = 0xFF;
        bufferOut[nrBytesOut] = value;
        nrBytesOut += 4; //value is 4 bytes long
    }
}

Last edited on
I was able to fix up the encryption program so it works as intended (I think). I was also able to make a decrypter for it. But you seem to have lost interest in this exercise.

Here's the encryption program. I left the globals and C-style arrays, so obviously that could be done better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <cstdint>
#include <cstdlib>

using byte = unsigned char;
using DWORD = uint32_t;

const DWORD MAX_BUFFER = 1000000;

byte  bufferIn[MAX_BUFFER];  // the content of the image
DWORD bytesIn = 0;           // number of bytes of image
byte  bufferOut[MAX_BUFFER]; // the content of the encrypted image
DWORD bytesOut = 0;          // number of bytes in encrypted image

void appendBufferOut(DWORD value) {
    if (value < 0xFF)
        bufferOut[bytesOut++] = value;
    else if (value < 0xFF00) {
        bufferOut[bytesOut++] = 0xFF;
        bufferOut[bytesOut++] = (value >> 8) & 0xFF;
        bufferOut[bytesOut++] = (value       & 0xFF);
    }
    else {
        bufferOut[bytesOut++] = 0xFF;
        bufferOut[bytesOut++] = 0xFF;
        bufferOut[bytesOut++] = (value >> 24) & 0xFF;
        bufferOut[bytesOut++] = (value >> 16) & 0xFF;
        bufferOut[bytesOut++] = (value >>  8) & 0xFF;
        bufferOut[bytesOut++] = (value        & 0xFF);
    }
}

int main(int argc, char **argv) {
    if (argc != 3) {
        std::cerr << "Usage: crazycryp INFILE OUTFILE\n";
        std::exit(EXIT_FAILURE);
    }

    std::ifstream fin(argv[1], std::ios::binary);
    if (!fin) {
        std::cerr << "Couldn't open input file\n";
        std::exit(EXIT_FAILURE);
    }

    for (char ch; fin.get(ch); )
        bufferIn[bytesIn++] = byte(ch);
    fin.close();

    for (int byteVal = 0; byteVal < 256; byteVal++) {
        DWORD count = 0;
        for (DWORD i = 0; i < bytesIn; i++)
            if (bufferIn[i] == byteVal)
                count++;
        if (count > 0) {
            appendBufferOut(byteVal);
            appendBufferOut(count);
            DWORD lastPos = 0;
            for (DWORD pos = 0; pos < bytesIn; pos++) {
                if (bufferIn[pos] == byteVal) {
                    appendBufferOut(pos - lastPos);
                    lastPos = pos;
                }
            }
        }
    }

    std::ofstream fout(argv[2], std::ios::binary);
    if (!fout) {
        std::cerr << "Couldn't open output file\n";
        std::exit(EXIT_FAILURE);
    }
    for (DWORD i = 0; i < bytesOut; i++)
        fout.put(bufferOut[i]);
    fout.close();
}

Last edited on
Thank you all for your replies, I was able to resolve it in the end.
Topic archived. No new replies allowed.