You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用密钥文件实现逐字节XOR加密及相关验证与解密?

使用密钥文件实现XOR加密的完整解决方案

我来帮你一步步解决这三个问题,结合你代码片段里的问题,给你清晰的实现思路和可运行的代码示例:

1. 实现输入文件与密钥文件的逐字节加密

你代码里的k = fgetc(k);是明显的错误——fgetc()需要传入文件指针,而不是变量k。正确的做法是同时打开明文文件、密钥文件和输出文件,然后循环读取两个文件的字节进行XOR操作。注意一定要用二进制模式打开文件("rb"/"wb"),避免文本模式下的换行符转换破坏数据。

完整的加密代码示例:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // 打开文件(替换成你的文件名)
    FILE *plain_file = fopen("plaintext.bin", "rb");
    FILE *key_file = fopen("key.bin", "rb");
    FILE *cipher_file = fopen("ciphertext.bin", "wb");

    // 检查文件是否成功打开
    if (!plain_file || !key_file || !cipher_file) {
        perror("Failed to open file(s)");
        return EXIT_FAILURE;
    }

    int plain_byte, key_byte;
    // 逐字节读取明文和密钥
    while ((plain_byte = fgetc(plain_file)) != EOF) {
        // 读取一个密钥字节
        if ((key_byte = fgetc(key_file)) == EOF) {
            fprintf(stderr, "Error: Key file is shorter than plaintext!\n");
            // 清理资源后退出
            fclose(plain_file);
            fclose(key_file);
            fclose(cipher_file);
            return EXIT_FAILURE;
        }
        // 执行XOR加密并写入输出文件
        int cipher_byte = plain_byte ^ key_byte;
        fputc(cipher_byte, cipher_file);
    }

    // 关闭所有文件
    fclose(plain_file);
    fclose(key_file);
    fclose(cipher_file);
    printf("Encryption completed successfully!\n");
    return EXIT_SUCCESS;
}

2. 验证密钥长度大于明文长度

有两种可靠的方式验证密钥长度:

方式一:加密前先计算文件大小

先获取明文和密钥文件的字节数,直接比较大小,不符合条件就终止加密:

// 辅助函数:获取文件大小(字节数)
long get_file_size(FILE *file) {
    fseek(file, 0, SEEK_END);
    long size = ftell(file);
    fseek(file, 0, SEEK_SET); // 重置文件指针到开头
    return size;
}

// 在加密前添加验证逻辑
long plain_size = get_file_size(plain_file);
long key_size = get_file_size(key_file);

if (key_size <= plain_size) {
    fprintf(stderr, "Error: Key file must be longer than plaintext!\n");
    fclose(plain_file);
    fclose(key_file);
    return EXIT_FAILURE;
}

方式二:加密后检查密钥剩余字节

在加密循环结束后,尝试读取密钥文件的下一个字节,如果能读到,说明密钥还有剩余(长度大于明文):

// 加密循环结束后执行
int extra_key_byte = fgetc(key_file);
if (extra_key_byte == EOF) {
    fprintf(stderr, "Warning: Key file is exactly the same length as plaintext!\n");
} else {
    printf("Success: Key file is longer than plaintext.\n");
}

方式一更高效,避免了不必要的加密操作;方式二则可以在加密过程中同时验证。

3. 用相同XOR操作恢复原始明文

完全可以!XOR运算的逆运算就是它本身——对密文再次使用相同的密钥文件进行XOR,就能还原出原始明文。原理很简单:
(明文 ^ 密钥) ^ 密钥 = 明文 ^ (密钥 ^ 密钥) = 明文 ^ 0 = 明文

解密代码和加密代码几乎一致,只需要替换输入输出文件:

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *cipher_file = fopen("ciphertext.bin", "rb");
    FILE *key_file = fopen("key.bin", "rb");
    FILE *decrypted_file = fopen("decrypted_plaintext.bin", "wb");

    if (!cipher_file || !key_file || !decrypted_file) {
        perror("Failed to open file(s)");
        return EXIT_FAILURE;
    }

    int cipher_byte, key_byte;
    while ((cipher_byte = fgetc(cipher_file)) != EOF) {
        if ((key_byte = fgetc(key_file)) == EOF) {
            fprintf(stderr, "Error: Key file is shorter than ciphertext!\n");
            fclose(cipher_file);
            fclose(key_file);
            fclose(decrypted_file);
            return EXIT_FAILURE;
        }
        // 执行XOR解密
        int plain_byte = cipher_byte ^ key_byte;
        fputc(plain_byte, decrypted_file);
    }

    fclose(cipher_file);
    fclose(key_file);
    fclose(decrypted_file);
    printf("Decryption completed successfully!\n");
    return EXIT_SUCCESS;
}

内容的提问来源于stack exchange,提问作者user

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:28:09