开发者

How can I use memfrob() to encrypt an entire file?

#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?


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
}


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜