tags:

views:

48

answers:

2
#include <Windows.h>

void memfrob(void * s, size_t n)
{
 char *p = (char *) s;

 while (n-- > 0)
  *p++ ^= 42;
}

int main()
{
 memfrob("C:\\Program Files\\***\***\\***\***\\***", 30344);
}

There's my code. If you can't tell, I'm not sure what I'm doing. I've Googled for about an hour and I haven't seen an example of how to use memfrob(), which is probably why I'm so lost. I'm trying to pass it the name of the file and then the size of the file in bytes, but my program just crashes.

Alright, this is what I have right now:

#include <Windows.h>
#include <stdio.h>

int count = 0;
FILE* pFile = 0;
long Size = 0;

void *memfrob(void * s, size_t n)
{
    char *p = (char *) s;

    while (n-- > 0)
        *p++ ^= 42;
    return s;
}

int main()
{
    fopen_s(&pFile, "C:\\Program Files\\CCP\\EVE\\lib\\corelib\\nasty.pyj", "r+");
    fseek(pFile, 0, SEEK_END);
    Size = ftell(pFile);
    char *buffer = (char*)malloc(Size);
    memset(buffer, 0, Size);
    fread(buffer, Size, 1, pFile);
    fclose(pFile);
    memfrob(buffer, Size);
    fopen_s(&pFile, "C:\\Program Files\\CCP\\EVE\\lib\\corelib\\nasty.pyj", "w+");
    fwrite(buffer, Size, 1, pFile);
    fclose(pFile);
}

In my debugger, it seems that fread is not writing anything to buffer, and my ending file is just 2A over and over, which is 00 xor'd with 42. So can I get another hint?

+1  A: 

Well, several things are wrong here. The minor problem is that you're passing it the location of the file and not the file itself. Read up on how to do file I/O in C (this being a pretty good link).

The real problem is that you seem to think this is encryption. This doesn't really encrypt your file from anything but the most trivial security issues (such as someone randomly opening your file).

Daniel Goldberg
If it's not encryption, then what is it?
Matti Virkkunen
@Matti It is, *technically*, encryption, but it is so weak that one'd argue it's obfuscation being disguised as encryption.
GSerg
Yeah, the data I'm trying to encrypt isn't that important and it won't be seen by many people. That's why memfrob() will be ok for me.
Chuy
+1  A: 

You need to pass memfrob a piece of memory containing the contents of the file, rather than the name of the file. It's crashing because you're passing in a buffer of read-only memory, and then trying to modify it.

Investigate the open and read I/O functions, or alternatively fopen and fread. Your mainline should look something like:

int main() {
    // open file 
    // find size of file
    // allocate buffer of that size
    // read contents of file into the buffer
    // close the file
    // call memfrob on the buffer
    // do what you want with the file
    // free the buffer
}
Graeme Perrow
Alright thanks, I pretty much followed that format, but now fread isn't writing to the buffer so I'm not sure what I messed up now.
Chuy