c - 对一个非常大的文件进行 XOR

标签 c file io stream xor

我想异或一个非常大的文件(~50 Go)。

更准确地说,我想通过使用 key 3847611839 对明文文件的每个 32 字节 block (由于内存不足)进行异或运算并创建(一个接一个地 block )一个新的密码文件。

感谢您的帮助!!

最佳答案

这听起来很有趣,而且不像家庭作业。

我没有以前异或加密的文件可以尝试,但如果你来回转换一个文件,就没有差异。

我至少试过了。享受! :) 这个 xor 每 4 个字节与 0xE555E5BF,我想这就是你想要的。

这里是 bloxor.c

// bloxor.c - by Peter Boström 2009, public domain, use as you see fit. :)

#include <stdio.h>

unsigned int xormask = 0xE555E5BF; //3847611839 in hex.

int main(int argc, char *argv[])
{
    printf("%x\n", xormask);
    if(argc < 3)
    {
        printf("usage: bloxor 'file' 'outfile'\n");
        return -1;
    }

    FILE *in = fopen(argv[1], "rb");
    if(in == NULL)
    {
        printf("Cannot open: %s", argv[2]);
        return -1;
    }

    FILE *out = fopen(argv[2], "wb");

    if(out == NULL)
    {
        fclose(in);
        printf("unable to open '%s' for writing.",argv[2]);
        return -1;
    }
    char buffer[1024]; //presuming 1024 is a good block size, I dunno...

    int count;

    while(count = fread(buffer, 1, 1024, in))
    {
        int i;
        int end = count/4;
        if(count % 4)
            ++end;

        for(i = 0;i < end; ++i)
        {
            ((unsigned int *)buffer)[i] ^= xormask;
        }
        if(fwrite(buffer, 1, count, out) != count)
        {
            fclose(in);
            fclose(out);

            printf("cannot write, disk full?\n");

            return -1;
        }
    }

    fclose(in);
    fclose(out);

    return 0;
}

关于c - 对一个非常大的文件进行 XOR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1656961/

相关文章:

c++ - 堆栈分配功能(性能)

c - 这个文件不应该以汇编语言开头吗

file - 如何使用 VS Code 以只读模式打开文件

c++ - 使用 fstream 写入文件将删除文件

c++ - C和C++文件读取性能比较

ios - 当主线程阻塞时,如何获得断点/日志/增加的可见性?

c - 从 Iup_FlatButton 中完全删除边框

java - 如何将文件从SpringBoot项目资源内的子文件夹复制到资源内的另一个子文件夹

java - 使用 Java IO 搜索并打印特定行

python - 在 Cygwin 中安装 xgboost 时缺少 execinfo.h