异或加密最简单因满足a^b^b==a,可流式逐块加解密;但非密码学安全,仅适用于低敏感场景,需避免内存溢出、符号扩展、硬编码密钥等问题。

为什么用异或(XOR)做文件加密最简单
因为异或满足 a ^ b ^ b == a,同一密钥反复异或即可加解密——无需额外算法库、不依赖第三方、不用处理 padding 或块模式。但注意:这只是“混淆”,不是密码学安全的加密,仅适用于临时保护明文配置、日志片段等低敏感场景。
常见错误是直接对整个文件一次性读入内存再异或,导致大文件(如 >500MB)崩溃或卡死;另一个坑是密钥长度为 1 时误用 char 类型导致符号扩展,比如密钥值 0xFF 被解释成 -1,异或结果错乱。
- 密钥建议用
unsigned char或std::uint8_t存储,避免符号问题 - 必须逐块读写(如 4KB~64KB),别用
std::ifstream::rdbuf()一次性加载 - 不要把密钥硬编码在可执行文件里——哪怕只是 demo,也该从命令行或环境变量传入
用 std::ifstream 和 std::ofstream 实现流式异或
核心是用 read()/write() 配合 gcount() 处理每次实际读取字节数,避免因文件末尾不足缓冲区大小导致的覆盖或丢字节。
int xor_encrypt_file(const std::string& input_path, const std::string& output_path, uint8_t key) {
std::ifstream fin(input_path, std::ios::binary);
std::ofstream fout(output_path, std::ios::binary);
if (!fin || !fout) return -1;
constexpr size_t BUFFER_SIZE = 8192;
std::vector buffer(BUFFER_SIZE);
while (fin.read(reinterpret_cast(buffer.data()), BUFFER_SIZE)) {
size_t bytes_read = fin.gcount();
for (size_t i = 0; i < bytes_read; ++i) {
buffer[i] ^= key;
}
fout.write(reinterpret_cast(buffer.data()), bytes_read);
}
// 处理剩余不足 BUFFER_SIZE 的部分
if (fin.gcount() > 0) {
size_t bytes_read = fin.gcount();
for (size_t i = 0; i < bytes_read; ++i) {
buffer[i] ^= key;
}
fout.write(reinterpret_cast(buffer.data()), bytes_read);
}
return 0;
}
这段代码能安全处理任意大小文件。关键点:gcount() 必须在每次 read() 后立即读取,不能用 fin.tellg() 推算——后者在二进制流中不可靠。
立即学习“C++免费学习笔记(深入)”;
支持多字节密钥的循环异或(更抗统计分析)
单字节密钥易被频率分析破解(比如大量 0x00 字节异或后变成固定值)。换成字符串密钥(如 "mykey123"),按位置循环使用每个字节,提升混淆强度。
注意点:
- 密钥字符串需转为
std::vector,避免std::string中的/0提前截断 - 索引用
i % key.size(),但要确保key非空,否则除零崩溃 - 不要用
std::string::c_str()直接取指针——遇到/0就停了
int xor_encrypt_file_multikey(const std::string& input_path, const std::string& output_path, const std::string& key_str) {
if (key_str.empty()) return -1;
std::vector key(key_str.begin(), key_str.end());
std::ifstream fin(input_path, std::ios::binary);
std::ofstream fout(output_path, std::ios::binary);
if (!fin || !fout) return -1;
constexpr size_t BUFFER_SIZE = 8192;
std::vector buffer(BUFFER_SIZE);
size_t key_idx = 0;
while (fin.read(reinterpret_cast(buffer.data()), BUFFER_SIZE)) {
size_t bytes_read = fin.gcount();
for (size_t i = 0; i < bytes_read; ++i) {
buffer[i] ^= key[key_idx++ % key.size()];
}
fout.write(reinterpret_cast(buffer.data()), bytes_read);
}
if (fin.gcount() > 0) {
size_t bytes_read = fin.gcount();
for (size_t i = 0; i < bytes_read; ++i) {
buffer[i] ^= key[key_idx++ % key.size()];
}
fout.write(reinterpret_cast(buffer.data()), bytes_read);
}
return 0;
}
命令行调用与常见报错处理
编译时加 -std=c++17(需要 std::byte 或 std::uint8_t),运行时若提示 No such file or directory,先确认路径是否含空格或中文——C++ 标准库对非 ASCII 路径支持差,Windows 下建议用 std::filesystem::u8path()(C++17)包装 UTF-8 字符串。
典型失败场景:
-
std::ofstream打开失败但没检查!fout→ 静默生成空文件 - 输入文件被其他程序占用(如 Excel 正在编辑 .csv)→
fin构造失败,is_open()返回 false - 输出路径父目录不存在 →
fout打开失败,不会自动创建目录
真正难调试的是跨平台换行符和 BOM:UTF-8 带 BOM 的文本文件头三个字节 0xEF 0xBB 0xBF 会被异或,解密后变成乱码,但你可能以为是密钥错了——其实只是忘了文件开头有 BOM。
