C XOR可执行文件加密/解密的问题

时间:2018-01-08 14:35:07

标签: c encryption xor

我试图在C中为.exe文件创建一个简单的XOR加密/解密器。我在C中仍然很新,并且还不了解所有内容,尤其是内存。所以我一直在关注如何制作一个简单的XOR字符串crypter的在线教程。现在我想修改它,以便我可以解密/解密可执行文件并决定使用fwrite()和fread()函数。这就是我提出的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // execve function

#define XOR_KEY 0xAA // key

#define JOB_CRYPT 1 // alter flow depending on the job
#define JOB_DECRYPT 2


//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

    void xorFile (char *infile, char *outfile) {

        FILE *nFile, *eFile;

        long nFileSize; // store file size of the file we want to read

        char *buffer; // buffer for reading
        char *eBuffer; // buffer for storing encrypted data

        size_t rResult;
        size_t wResult;

        ///// READ FILE /////

        nFile = fopen(infile, "rb");

        if(nFile == NULL) {

            fputs("Error opening file...", stderr);
            exit(EXIT_FAILURE);

        }

        fseek(nFile, 0, SEEK_END);
        nFileSize = ftell(nFile);
        rewind(nFile);

        buffer = (char *) malloc(sizeof(char) * nFileSize);

        if(buffer == NULL) {

            fputs("Error allocating memory...", stderr);
            exit(EXIT_FAILURE);

        }

        rResult = fread(buffer, 1, nFileSize, nFile);

        if(rResult != nFileSize) {

            fputs("Error reading file...", stderr);
            exit(EXIT_FAILURE);

        }

        fclose(nFile);

        printf("File size is: %ld\n", nFileSize);
        printf("Buffer size is (pointer): %u\n", sizeof(buffer));
        printf("Reading result: %lu\n", rResult);

        ////// WRITE TO FILE //////

        eFile = fopen(outfile, "wb");


        if(eFile == NULL) {

            fputs("Error creating file...", stderr);
            exit(EXIT_FAILURE);

        }

        eBuffer = (char *) malloc(sizeof(char) * nFileSize);

        if(eBuffer == NULL) {

            fputs("Error allocating memory (2)...", stderr);
            exit(EXIT_FAILURE);

        }

        // encrypt byte by byte and save to buffer
        printf("Proceeding with encryption!\n");

        for(int i = 0; buffer[i] != EOF; i++) {

            eBuffer[i] = buffer[i] ^ XOR_KEY;

        }

        printf("Proceeding with fwrite()!\n");

        wResult = fwrite(eBuffer, 1, nFileSize, eFile);

        fclose(eFile);

        printf("eBuffer size is (pointer)%u\n", sizeof(eBuffer));
        printf("Writing result: %lu\n", wResult);

        free(buffer); // free buffers in heap
        free(eBuffer);

    }


//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////


int main(int argc, char *argv[]) {

    // checking if all parameters were given

    if(argc < 4) {

        fprintf(stderr, "Usage: %s [CRYPT | DECRYPT] [IN-FILE] [OUT-FILE]\n", argv[0]);
        exit(EXIT_FAILURE);

    }

    int job;

    // DOLOCIMO JOB

    if(strcmp(argv[1], "CRYPT") == 0) {

        job = JOB_CRYPT;

    } else if (strcmp(argv[1], "DECRYPT") == 0) {

        job = JOB_DECRYPT;

    } else {

        fprintf(stderr, "Please select [CRYPT | DECRYPT]!");
        exit(EXIT_FAILURE);

    }

    // CRYPT/DECRYPT OUR FILE

    xorFile(argv[2], argv[3]);


    if(job == JOB_DECRYPT) {

        char *args[] = {argv[3], NULL};

        int errExec = execve(args[0], args, NULL);

        if(errExec == -1) {

            perror("Error executing file...");
            exit(EXIT_FAILURE);

        }

    }


    return 0;
}

我很抱歉看起来丑陋的代码,但我首先想让它工作,我稍后会对它进行改进。

无论如何,当我在命令提示符下运行它时,加密工作正常,它会生成一个加密文件,但是当我运行decrpytion作业时,程序 在解密过程中崩溃。这是一张发生了什么的图片,所以你可以想象它更好。

由于我的声誉不到10,我不允许嵌入图片。 Here is a link to Imgur

这里出了什么问题?我在解密时会创建缓冲区溢出吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于:

    for(int i = 0; buffer[i] != EOF; i++) {
        eBuffer[i] = buffer[i] ^ XOR_KEY;
    }

二进制文件可以包含任何值的字节。因此EOF值有效,并且不指定文件的结尾。这意味着如果文件包含具有此值的字节,则循环将提前退出并且您不会对所有字节进行异或。如果文件不包含此字节,则循环将超过分配的内存的末尾,该内存将调用undefined behavior,在这种情况下会显示崩溃。

您知道需要处理多少字节,因此请将其用作循环控件:

    for(int i = 0; i < nFileSize; i++) {

其他一些小修正:

buffer = (char *) malloc(sizeof(char) * nFileSize);
if(buffer == NULL) {
    fputs("Error allocating memory...", stderr);
    exit(EXIT_FAILURE);
}

Don't cast the return value of malloc。此外,根据定义,sizeof(char)为1,因此您可以将其保留。

此外,如果系统或库函数失败,您应使用perror来打印错误消息。这将打印有关函数失败原因的其他信息。

buffer = malloc(nFileSize);
if(buffer == NULL) {
    perror("Error allocating memory...");
    exit(EXIT_FAILURE);
}
相关问题